What Are Strings in Python?

Strings are sequences of characters — letters, numbers, or symbols — enclosed in single ('...') or double ("...") quotes. They’re one of the most common data types in Python.

1. Basic String Printing

print("Hello, World!") # Double quotes
print('Hello, World!') # Single quotes

2. Assigning Strings to Variables

greeting = "Hello, World!"
print(greeting)

3. Concatenating Strings

You can join (concatenate) strings with +:
name = "Alice"
print(greeting + " My name is " + name + ".")

4. Formatting Strings

Use f-strings to insert variables inside a string:
age = 30
print(f"My name is {name} and I am {age} years old.")

5. Escape Characters

The backslash \ lets you add special characters:
print("Hello, \"World\"!") # Quotes inside quotes
print("Hello\nWorld!") # Newline
print("Hello\tWorld!") # Tab


Output:

Hello, "World"!
Hello
World!
Hello World!

6. Indexing Strings

You can access individual characters:
word = "Python"
print(word[0]) # 'P'
print(word[1]) # 'y'
print(word[-1]) # 'n' (last character)

7. Slicing Strings

Get parts of a string:
print(word[0:2]) # 'Py'
print(word[2:]) # 'thon'
print(word[:3]) # 'Pyt'

8. Iterating Over Strings

for char in word:
print(char, end=' ')
Output: P y t h o n

9. Checking Membership

Check if a character is in a string:
print('P' in word) # True
print('z' in word) # False

10. Changing Case

print(word.upper()) # 'PYTHON'
print(word.lower()) # 'python'

Putting It All Together: Real-World Examples

You’ll see these string basics in real Python tasks all the time. For example:

  • Display messages: When building a simple CLI app, you’ll print() a greeting, ask for a name, then say “Hello, Alice!” using string concatenation or f-strings.
  • Format output: If you’re writing a script that shows someone’s age or balance, you’ll format strings with variables to make the message clear and readable.
  • Escape tricky characters: If your app needs to handle file paths (C:\Users\Alice\Documents) or quotes in text, you’ll use the \ escape character to keep your strings valid.
  • Work with user input: When processing names, email addresses, or commands, you’ll slice or index strings — for example, get just the domain from an email with email.split('@')[1].
  • Check conditions: Want to see if a username includes spaces or forbidden symbols? You’ll loop through characters or check membership with ' ' in username.
  • Clean up text: Reading text from files or scraping websites often leaves extra spaces or line breaks — strip(), upper(), and lower() help you tidy up and standardize that text.

From printing “Hello, World!” to building your first real script, these simple techniques are the backbone of handling text in Python — and they show up in projects of all sizes.

Please see the next post.

For practice, I recommend this website. (A sandbox python practice panel will be added soon. Stay tuned!)

For the next post, click here.

Scroll to Top