Python Strings
Strings are one of the most important data types in Python. They are used to store text — anything from a single character to a whole block of paragraphs.
In this tutorial, you’ll learn everything you need to work with strings in Python, including how to create them, manipulate them, and use built-in methods effectively.
What is a String in Python?
A string in Python is a sequence of characters enclosed in single, double, or even triple quotes.
# Examples of strings
text1 = 'Hello'
text2 = "World"
text3 = '''This is a multi-line string.'''
String Variables
You can assign a string to a variable like this:
greeting = "Hello, Python!"
print(greeting)
Multiline Strings
Use triple quotes ('''
or """
) to write a string across multiple lines:
message = """Python is fun,
and it's great for beginners!"""
print(message)
String Length
Use len()
to get the length of a string:
name = "Python"
print(len(name)) # Output: 6
Accessing Characters in a String
Python treats strings like arrays of characters. You can access individual characters using indexing:
txt = "Python"
print(txt[0]) # Output: P
print(txt[-1]) # Output: n (last character)
Looping Through a String
You can loop through each character in a string:
for char in "Hello":
print(char)
Check if Substring Exists
Use the in
keyword:
txt = "Learning Python is fun"
print("Python" in txt) # True
To check if a substring does not exist, use not in
:
print("Java" not in txt) # True
String Slicing
Extract parts of a string using slicing:
text = "Python Programming"
print(text[0:6]) # Output: Python
print(text[:6]) # Output: Python
print(text[7:]) # Output: Programming
String Methods
Python has a variety of built-in methods to manipulate strings:
txt = " Hello World "
print(txt.strip()) # Removes surrounding whitespace
print(txt.lower()) # hello world
print(txt.upper()) # HELLO WORLD
print(txt.replace("H", "J")) # Jello World
print(txt.split()) # ['Hello', 'World']
Use
dir(str)
in Python to see all available string methods.
String Concatenation
Join two strings with the +
operator:
a = "Hello"
b = "World"
print(a + " " + b) # Output: Hello World
String Formatting
Use f-strings
(recommended in Python 3.6+) or .format()
for dynamic content:
name = "Alice"
age = 30
print(f"My name is {name}, and I am {age} years old.")
# Output: My name is Alice, and I am 30 years old.
Escape Characters
Use escape characters for special symbols like quotes, newlines, etc.
txt = "He said, \"Python is awesome!\""
print(txt)
# Newline and Tab
print("Line1\nLine2")
print("Name:\tJohn")
Summary
Task | Method |
---|---|
Get length | len(string) |
Access character | string[index] |
Slice string | string[start:end] |
Check substring | "sub" in string |
Concatenate | str1 + str2 |
Format string | f"Hello {name}" |
String methods | .lower() , .upper() , .strip() , .replace() , .split() |
Final Words
Strings are the foundation of text manipulation in Python. Whether you're building websites, automating scripts, or handling user input, mastering strings is essential.