Master Python String Operations: Replace, Join, Split, Reverse, and Case Conversion
In Python, every entity is an object—including strings. A string is simply a sequence of characters enclosed in quotes.
Example:var = "Hello World!"
This guide walks you through the most common string operations, backed by real examples and best‑practice tips.
- Accessing Characters with Slicing
- String Operators and Concatenation
- Replacing Substrings
- Changing Case (upper/lower/capitalize)
- Using
joinfor Flexible Concatenation - Reversing Strings
- Splitting Strings
Accessing Characters in Strings
Python does not have a distinct character type; single characters are simply one‑length strings. Use square brackets and indices to slice.
var1 = "Guru99!"
var2 = "Software Testing"
print("var1[0]:", var1[0])
print("var2[1:5]:", var2[1:5])
Output:var1[0]: G
var2[1:5]: oftw
String Operators
Operators enable quick manipulation:
| Operator | Description | Example |
|---|---|---|
| [] | Access a single character | x = "Guru" |
| [:] | Slice a range (end exclusive) | x = "Guru" |
| in / not in | Check membership | print("u" in x) — True |
| r/R | Raw string literal (escape characters ignored) | print(r"\n") — outputs "\\n" |
| % | String formatting (old style) | print('%s %d' % ('guru', 99)) — "guru 99" |
| + | Concatenate two strings | print("Guru" + "99") — "Guru99" |
| * | Repeat string | print("Guru" * 2) — "GuruGuru" |
Updating a String
Because strings are immutable, re‑assign the variable to a new value.
x = "Hello World!" print(x[:6]) # "Hello" print(x[0:6] + "Guru99") # "Hello Guru99"
Replacing Substrings
The replace() method returns a new string with all occurrences of a substring replaced.
old = "I like Guru99"
new = old.replace('like', 'love')
print(new) # "I love Guru99"
Changing Case
Convert to upper, lower, or capitalize the first character.
text = "python at guru99" print(text.upper()) # "PYTHON AT GURU99" print(text.capitalize()) # "Python at guru99" print(text.upper().lower()) # back to lower case
Joining Characters
The join() method concatenates an iterable of strings using a separator.
print(":".join("Python")) # "P:y:t:h:o:n"
Reversing a String
Use reversed() and join() to reverse.
s = "12345"
print(''.join(reversed(s))) # "54321"
Splitting Strings
Break a string into a list based on a delimiter.
sentence = "guru99 career guru99"
print(sentence.split(' ')) # ['guru99', 'career', 'guru99']
print(sentence.split('r')) # ['gu', 'u99 ca', 'ee', ' gu', 'u99']
Immutability Reminder
Strings cannot be altered in place. Methods like replace() return a new string.
x = "Guru99"
print(x.replace("Guru99", "Python")) # still "Guru99"
# To change the variable
x = x.replace("Guru99", "Python")
print(x) # "Python"
Python 2 Compatibility
Below is the equivalent code for Python 2, using print statements without parentheses.
# Accessing values
var1 = "Guru99!"
var2 = "Software Testing"
print "var1[0]:", var1[0]
print "var2[1:5]:", var2[1:5]
# Update example
x = "Hello World!"
print x[:6]
print x[0:6] + "Guru99"
# Replace example
old = 'I like Guru99'
print old.replace('like', 'love')
# Case conversion
text = "python at guru99"
print text.upper()
print text.capitalize()
print text.lower()
# Join
print ":".join("Python")
# Reverse
s = "12345"
print ''.join(reversed(s))
# Split
word = "guru99 career guru99"
print word.split(' ')
print word.split('r')
# Immutability demo
x = "Guru99"
print x.replace("Guru99", "Python")
x = x.replace("Guru99", "Python")
print x
Summary
- Use slicing (
[],[:]) for precise character access. - Leverage string operators (+, *, in, not in) for concatenation and membership checks.
- Apply
replace()to substitute substrings; remember it returns a new string. - Convert case with
upper(),lower(),capitalize(). - Combine
join()with any separator for flexible concatenation. - Reverse with
reversed()andjoin(), split withsplit(). - Always re‑assign to capture changes due to string immutability.
Python
- Mastering Python I/O and Module Imports: A Practical Guide
- Master Python Strings: Creation, Formatting, and Manipulation
- Mastering Java Strings: Creation, Methods, and Best Practices
- C Strings Made Easy: Declaration, Input, Output, and Library Functions
- Python Print() Function: A Practical Guide with Examples
- Mastering Python’s strip() Method: Comprehensive Guide & Practical Examples
- Master Python’s str.count(): How to Count Characters & Substrings with Examples
- C Strings: How Null-Terminated Character Arrays Work
- Mastering Strings in C#: Best Practices & Tips
- Mastering Python Strings: Creation, Access, and Manipulation