๐Ÿงต 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])  # Programmin

3. Reversing a String ๐Ÿ”„

reverse.py

text = "Python"
print(text[::-1])   # nohtyP

4. 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 only

7. 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"))    # 2

9. 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()) # True

14. 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! ๐Ÿ˜Š