Creating and Inspecting Strings
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.
123title = "Python 101" emoji = "🙂" print(title, emoji)
Creating Strings
Python lets you write string literals with single or double quotes; choose whichever makes the code clearer.
123456a = "hello" b = 'he said: "hi"' c = "it's fine to mix quotes like this" print(a) print(b) print(c)
If you need multiple lines, use triple-quoted strings. Python keeps the line breaks.
123message = """Line 1 Line 2""" print(message)
To include special characters (like a newline) in a single line, use escape sequences.
12greet = "hello\nworld" # \n is a single newline character print(greet)
When backslashes should be taken literally (e.g., Windows paths or simple regex patterns), a raw string helps.
12path = r"C:\Users\alex\docs" # backslashes are not treated as escapes print(path)
Inspecting Strings (without indexing yet)
You'll often need quick checks: "what is this?" and "how long is it?"
123s = "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.
12345name = "" if name: print("Welcome,", name) else: print("Please enter your name") # runs because "" is falsey
""
(empty) is not the same as" "
(a space).len("") == 0
,len(" ") == 1
;\n
counts as one character (a newline), solen("A\nB") == 3
;- Prefer raw strings for paths to avoid accidental escapes like
"\t"
(tab).
1. Which is a valid string literal in Python?
2. What value will this code output?
3. Which statement is true?
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you explain more about string immutability?
What are some common string operations in Python?
How do I handle special characters in strings?
Awesome!
Completion rate improved to 5
Creating and Inspecting Strings
Veeg om het menu te tonen
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.
123title = "Python 101" emoji = "🙂" print(title, emoji)
Creating Strings
Python lets you write string literals with single or double quotes; choose whichever makes the code clearer.
123456a = "hello" b = 'he said: "hi"' c = "it's fine to mix quotes like this" print(a) print(b) print(c)
If you need multiple lines, use triple-quoted strings. Python keeps the line breaks.
123message = """Line 1 Line 2""" print(message)
To include special characters (like a newline) in a single line, use escape sequences.
12greet = "hello\nworld" # \n is a single newline character print(greet)
When backslashes should be taken literally (e.g., Windows paths or simple regex patterns), a raw string helps.
12path = r"C:\Users\alex\docs" # backslashes are not treated as escapes print(path)
Inspecting Strings (without indexing yet)
You'll often need quick checks: "what is this?" and "how long is it?"
123s = "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.
12345name = "" if name: print("Welcome,", name) else: print("Please enter your name") # runs because "" is falsey
""
(empty) is not the same as" "
(a space).len("") == 0
,len(" ") == 1
;\n
counts as one character (a newline), solen("A\nB") == 3
;- Prefer raw strings for paths to avoid accidental escapes like
"\t"
(tab).
1. Which is a valid string literal in Python?
2. What value will this code output?
3. Which statement is true?
Bedankt voor je feedback!