Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Creating and Inspecting Strings | Section
Working with Numbers in Python: Integers, Floats, and Type Conversion - 1769704232138

Creating and Inspecting Strings

Pyyhkäise näyttääksesi valikon

Strings are how Python stores text - names, messages, file paths, even emoji. In Python 3, strings are Unicode by default, so they can represent characters from virtually any language.

What Is a String?

A string is an immutable sequence of characters. "Immutable" means you don't change a string in place. Instead, operations create a new string.

12345
# Displaying a course title with an icon course_title = "Data Types in Python" status_emoji = "🚀" print(course_title, status_emoji) # Output: Python for Beginners 🚀

Creating Strings

Python lets you write string literals with single or double quotes. Choose whichever makes the code clearer.

12345678
# Showing how to handle quotes correctly in user messages greeting = "Hello!" message = 'User said: "Hi, nice to meet you!"' note = "It's fine to mix single and double quotes in text." print(greeting) print(message) print(note)

If you need multiple lines, use triple-quoted strings. Python keeps the line breaks.

123456
# Creating a multi-line email template email_template = """Dear User, Your subscription has been successfully renewed. Thank you for staying with us!""" print(email_template)

To include special characters (like a newline) in a single line, use escape sequences.

123
# Formatting a welcome message for the user welcome_message = "Welcome to Codefinity!\nLet's start your learning journey." print(welcome_message)

When backslashes should be taken literally (e.g., Windows paths or simple regex patterns), a raw string helps.

123
# Storing a file path on a Windows system file_path = r"C:\Users\alex\Documents\report.pdf" # backslashes are kept as-is print(file_path)

Inspecting Strings (without indexing yet)

You'll often need quick checks: "what is this?" and "how long is it?"

123
s = "hello" print(type(s)) # <class 'str'> print(len(s)) # 5

An empty string is considered falsey, while any non-empty string is truthy. That's handy in conditions.

1234567
# Checking if the user entered their username during login username = "" if username: print(f"Welcome back, {username}!") else: print("Please enter your username") # runs because an empty string is falsey
Note
Note
  • "" (empty) is not the same as " " (a space). len("") == 0, len(" ") == 1;
  • \n counts as one character (a newline), so len("A\nB") == 3;
  • Prefer raw strings for paths to avoid accidental escapes like "\t" (tab).
question mark

Which is a valid string literal in Python?

Valitse oikea vastaus

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 13

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 1. Luku 13
some-alt