๐งต Python String Manipulation โ Transforming & Handling Text Like a Pro
Introduction ๐
String manipulation refers to modifying, transforming, analyzing, or extracting information from strings. Python provides powerful tools โ slicing, methods, formatting, concatenation, and more โ to make text processing easy.
Note
๐ก Mastering string manipulation is essential for data cleaning, user input handling, automation, and building real-world applications.
1. String Indexing ๐ฏ
Use indexing to access individual characters.
indexing.py
text = "Python"
print(text[0]) # P
print(text[3]) # h
print(text[-1]) # n (last character)Note
Indexing starts at 0. Negative indexing starts from the end.
2. String Slicing โ๏ธ
Extract portions of a string using slicing.
slicing.py
word = "Python Programming"
print(word[0:6]) # Python
print(word[:6]) # Python
print(word[7:]) # Programming
print(word[-11:-1]) # Programmin3. Reversing a String ๐
reverse.py
text = "Python"
print(text[::-1]) # nohtyP4. Concatenation โ
Combine strings using +.
concat.py
first = "Hello"
second = "World"
print(first + " " + second)5. Repetition ๐
repeat.py
print("Hi! " * 3) # Hi! Hi! Hi!6. Removing Spaces ๐งผ
strip_functions.py
msg = " hello python "
print(msg.strip()) # both sides
print(msg.lstrip()) # left only
print(msg.rstrip()) # right only7. Changing Case ๐ค
case_methods.py
text = "hello python"
print(text.upper())
print(text.lower())
print(text.title())
print(text.capitalize())
print(text.swapcase())8. Searching & Checking Substrings ๐
search.py
text = "Python programming"
print("Python" in text) # True
print(text.find("gram")) # 10
print(text.count("m")) # 29. Replace Text ๐
replace.py
msg = "I love Java"
print(msg.replace("Java", "Python"))10. Splitting & Joining Strings ๐งฉ
split()
split.py
msg = "hello python world"
words = msg.split()
print(words)join()
join.py
words = ['hello', 'python', 'world']
print("-".join(words))11. Removing Characters โจ
remove_chars.py
text = "Hello!!!"
clean = text.replace("!", "")
print(clean)12. String Formatting ๐งท
f-Strings (Best Option)
fstring.py
name = "Sathish"
age = 25
print(f"My name is {name} and I am {age} years old.")format()
format_method.py
print("Name: {}, Age: {}".format("Sathish", 25))13. Checking String Properties โ๏ธ
property_checks.py
text = "Python123"
print(text.isalpha()) # False
print(text.isdigit()) # False
print(text.isalnum()) # True
print(" ".isspace()) # True14. Converting Between Strings & Other Types ๐
type_casting.py
num = 10
text = str(num)
print(text, type(text))
digits = "123"
print(int(digits) + 10)15. Extracting Words / Characters ๐ฏ
extract.py
sentence = "Python is powerful"
first_word = sentence.split()[0]
print(first_word) # Python
chars = list(sentence)
print(chars[:6]) # ['P','y','t','h','o','n']16. Real-World Example ๐
real_world.py
email = input("Enter email: ").strip()
username = email.split("@")[0]
domain = email.split("@")[1]
print(f"Username: {username}")
print(f"Domain: {domain}")Conclusion ๐
>>"String manipulation is the art of shaping text โ master it to unlock real power in Python." โจ
You now know all essential string manipulation techniques. Want a tutorial on Operators, Expressions, Conditional Statements, Loops, Functions, Lists, Tuples, Dictionaries? Just tell me the next topic! ๐