Strings
Working with text — creating, combining, indexing, and understanding strings in Python.
"Every message, every name, every sentence your program works with is a string. Text is the most human thing in code."
— ShurAIWhat is a String?
A string is a piece of text — a sequence of characters wrapped in quotes. Characters include letters, digits, spaces, and punctuation. You tell Python something is text simply by putting quotes around it.
# Single quotes
name = 'Priya'
# Double quotes — same result
city = "Mumbai"
# Triple quotes — for multi-line text
message = """Hello!
Welcome to ShurAI.
Start learning Python today."""
print(type(name)) # <class 'str'>
Both work identically. The only time it matters: if your text contains an apostrophe like don't, use double quotes: "don't". If it contains double quotes, use single quotes around it.
Combining Strings — Concatenation
Use + to join strings, and * to repeat a string:
first = "Aarav"
last = "Shah"
full = first + " " + last
print(full) # Aarav Shah
print("Ha" * 3) # HaHaHa
print("-" * 20) # --------------------
"Score: " + 95 causes a TypeError. You must convert first: "Score: " + str(95). Or use f-strings (Topic 10) which handle this automatically and cleanly.
String Length with len()
Count every character — letters, spaces, punctuation — all count:
print(len("Hello")) # 5
print(len("Hello World")) # 11 — space counts!
print(len("")) # 0 — empty string
Accessing Characters — Indexing
Every character has a position called an index. Python always starts from 0, not 1. Negative indexes count backwards from the end.
word = "Python"
print(word[0]) # P — first character
print(word[5]) # n — last character
print(word[-1]) # n — last character (negative)
print(word[-2]) # o — second from last
Extracting Parts — Slicing
Get a portion of a string using [start:end]. Start is included, end is not included:
text = "Hello World"
print(text[0:5]) # Hello — index 0,1,2,3,4
print(text[6:]) # World — index 6 to end
print(text[:5]) # Hello — start to index 4
print(text[-5:]) # World — last 5 characters
print(text[::-1]) # dlroW olleH — reversed!
Escape Characters
Special characters you write with a backslash inside a string:
print("Line1\nLine2") # \n = new line
print("Name:\tRiya") # \t = tab space
print("She said \"Hi\"") # \" = quote inside string
print("C:\\Users\\Riya") # \\ = actual backslash
Real Example — Name Tag Generator
first = "Sneha"
last = "Kulkarni"
full = first + " " + last
print("=" * 24)
print("Full name :", full)
print("Initials :", first[0] + "." + last[0] + ".")
print("Length :", len(full))
print("Reversed :", full[::-1])
========================
Full name : Sneha Kulkarni
Initials : S.K.
Length : 14
Reversed : inrakluK ahnenS
"Strings are everywhere in real programs — usernames, messages, search queries. Master them and your programs become truly expressive."
— ShurAI🧠 Quiz — Question 1
What index does the first character of a Python string have?
🧠 Quiz — Question 2
What does "Go!" * 3 produce?
🧠 Quiz — Question 3
What does "Python"[-1] return?
🧠 Quiz — Question 4
What does "Hello World"[0:5] return?