Table of Contents
We’ve already met strings briefly — they showed up when we printed ‘Hello, World!’ and when we stored names in variables. But strings are so commonly used in Python that they genuinely deserve their own post.
Almost every real-world program works with text in some form — usernames, messages, file names, URLs. Understanding strings well early on will make everything else significantly easier.
What is a String?
A string is simply a sequence of characters — letters, numbers, symbols, spaces — wrapped in quotes. Single or double, Python doesn’t mind which you use, as long as you’re consistent within a single string.
greeting = "Hello, World!"
city = 'Mumbai'
mixed = "It's a great day"
Notice that third example — we used double quotes on the outside so we could have a single quote (apostrophe) inside without confusing Python. Smart, right?
Multi-line Strings
What if your text spans multiple lines? Use triple quotes — either “”” or ”’:
message = """
Hello,
This is a multi-line string.
Pretty useful!
"""
Python preserves every line break exactly as you typed it. Useful for long messages, formatted text, or anything that naturally spans multiple lines.
String Concatenation
Concatenation is just a fancy word for joining strings together. You do it with the + operator:
first = "Hello"
second = "World"
result = first + " " + second
print(result) # Hello World
Notice we added a space in the middle — ” “ — otherwise it would print “HelloWorld” with no gap. Python joins exactly what you give it, nothing more.
String Repetition
You can also repeat a string using *:
line = "-" * 20
print(line) # --------------------
Small trick, but surprisingly handy when you want to print dividers or separators.
String Length
Want to know how many characters are in a string? Use len():
name = "Aryan"
print(len(name)) # 5
Spaces count too — “Hello World” has a length of 11, not 10.
Accessing Characters in a String
Every character in a string has a position — called an index. And Python starts counting from 0, not 1. So in the string “Aryan”:
name = "Aryan"
print(name[0]) # A
print(name[1]) # r
print(name[4]) # n
The 0-based counting trips up a lot of beginners — just remember, the first character is always at index 0. Not 1. Always 0.
Changing Case
Python has built-in methods to change the case of strings:
name = "aryan"
print(name.upper()) # ARYAN
print(name.lower()) # aryan
print(name.title()) # Aryan
These are incredibly useful when comparing user inputs — someone might type ‘ARYAN’ or ‘aryan’ or ‘Aryan’, and you want to treat them all the same.
Quick Recap
- Strings are text wrapped in single or double quotes
- Triple quotes handle multi-line strings
- + joins strings, * repeats them
- len() gives the length — spaces count too
- String indexing starts at 0, not 1
- .upper(), .lower(), .title() change the case
Strings are everywhere in programming, and we’ve only scratched the surface here. There’s a whole world of string methods in Python — but this gives you a solid foundation to build on. Next up — Comments in Python. Short, sweet, and genuinely underrated.
