Creación e Inspección de Cadenas
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).
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Can you explain more about escape sequences in strings?
How do I use triple quotes for multi-line strings?
What does it mean that strings are immutable in Python?
Genial!
Completion tasa mejorada a 3.45
Creación e Inspección de Cadenas
Desliza para mostrar el menú
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).
¡Gracias por tus comentarios!