Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Creating and Inspecting Strings | Strings
Data Types in Python

bookCreating 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 πŸš€
copy

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)
copy

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)
copy

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)
copy

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)
copy

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
copy

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
copy
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?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

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

bookCreating 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 πŸš€
copy

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)
copy

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)
copy

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)
copy

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)
copy

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
copy

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
copy
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?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1
some-alt