Strings Basics
What You'll Learn
- What strings are and how to create them
- Single quotes vs. double quotes
- Escape characters for special symbols
- String concatenation and repetition
- Finding the length of a string with
len() - Accessing individual characters with indexing
1. What is a String?
Strings are one of the most common data types in programming. Almost every program works with text in some way, whether it is displaying a message to the user, storing a name, or processing a document.
greeting = "Hello, World!" name = "Alice" address = "123 Main Street" empty = "" # An empty string (has zero characters) print(greeting) print(type(greeting))
2. Single Quotes vs. Double Quotes
Python lets you create strings using either single quotes (') or double quotes ("). They work exactly the same way:
message1 = 'Hello, World!' message2 = "Hello, World!" print(message1 == message2) # Are they equal?
So when should you use one over the other? The main reason is when your string contains a quote character:
# Use double quotes when your string contains a single quote sentence = "It's a beautiful day!" print(sentence) # Use single quotes when your string contains double quotes quote = 'She said "hello" to everyone.' print(quote)
'It's a beautiful day!', Python sees the apostrophe as the end of the string and gets confused by the remaining text. Use double quotes to avoid this problem.
3. Escape Characters
Sometimes you need to include special characters in a string. Python uses the backslash (\) as an escape character to give characters special meaning:
| Escape Sequence | Meaning | Example |
|---|---|---|
\' | Single quote | 'It\'s fun' |
\" | Double quote | "She said \"hi\"" |
\\ | Backslash | "C:\\Users\\Alice" |
\n | New line | "Line 1\nLine 2" |
\t | Tab | "Name:\tAlice" |
# Using escape characters print("She said \"hello\" to me.") print('It\'s a great day!') print("First line\nSecond line") print("Name:\tAlice") print("File path: C:\\Users\\Documents")
You can also create multi-line strings using triple quotes:
poem = """Roses are red, Violets are blue, Python is awesome, And so are you!""" print(poem)
4. String Concatenation (+)
You can join strings together using the + operator. This is called concatenation:
first_name = "Alice" last_name = "Smith" full_name = first_name + " " + last_name print(full_name)
TypeError.
# This will cause an error! # age = 25 # message = "I am " + age + " years old" # TypeError! # Fix: convert the number to a string first age = 25 message = "I am " + str(age) + " years old" print(message)
5. String Repetition (*)
You can repeat a string multiple times using the * operator:
line = "-" * 30 print(line) cheer = "Go! " * 3 print(cheer) pattern = "*-" * 10 print(pattern)
Practical Use: Creating Formatted Output
title = "Report Card" border = "=" * 30 print(border) print(title) print(border)
6. String Length and Indexing
The len() function tells you how many characters are in a string:
name = "Alice" print(len(name)) # 5 message = "Hello, World!" print(len(message)) # 13 (spaces and punctuation count!) empty = "" print(len(empty)) # 0
You can access individual characters using indexing. Python uses zero-based indexing, meaning the first character is at position 0:
word = "Python" # P y t h o n # Index: 0 1 2 3 4 5 print(word[0]) # P (first character) print(word[1]) # y (second character) print(word[5]) # n (last character) print(word[-1]) # n (last character using negative index) print(word[-2]) # o (second to last)
Check Your Understanding
Question 1: What will len("Hello\nWorld") return?
Answer: 11
The \n escape sequence counts as a single character (a newline), so: H-e-l-l-o-\n-W-o-r-l-d = 11 characters.
Question 2: Given text = "Python", what is text[2]?
Answer: "t"
Index 0 is "P", index 1 is "y", index 2 is "t".
Try It Yourself!
- Create a string with your full name. Print it and its length.
- Create a string that contains both single and double quotes.
- Use
+to build a greeting that says "Hello, [your name]! Welcome!" - Print a line of 50 asterisks using
*repetition.
Key Takeaways
- Strings are sequences of characters enclosed in quotes.
- Single quotes and double quotes are interchangeable; use whichever avoids conflicts with the string content.
- Escape characters (
\n,\t,\\, etc.) represent special characters. - Use
+for concatenation and*for repetition. len()returns the number of characters in a string.- Python uses zero-based indexing: the first character is at index 0.