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.
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?"
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.
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
""(empty) is not the same as" "(a space).len("") == 0,len(" ") == 1;\ncounts as one character (a newline), solen("A\nB") == 3;- Prefer raw strings for paths to avoid accidental escapes like
"\t"(tab).
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain more about string immutability in Python?
What are some common escape sequences used in strings?
How do I use raw strings for regular expressions?
Awesome!
Completion rate improved to 5.26
Creating and Inspecting Strings
Swipe to show menu
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?"
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.
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
""(empty) is not the same as" "(a space).len("") == 0,len(" ") == 1;\ncounts as one character (a newline), solen("A\nB") == 3;- Prefer raw strings for paths to avoid accidental escapes like
"\t"(tab).
Thanks for your feedback!