---Advertisement---

Python Strings Manipulation: Replace, Join, Split, Reverse, Uppercase & Lowercase Explained Great 2025

By Bhavani

Updated On:

---Advertisement---
 Python Strings Manipulation

πŸ‘‰Tutorial-2: Python strip() Function
πŸ‘‰Tutorial-3:  Python count() Function
πŸ‘‰Tutorial-4:  Python format() String Method
πŸ‘‰Tutorial-5: Python len() Function
πŸ‘‰Tutorial-6: Python String find() Method
πŸ‘‰Tutorial-7: Python String Split() Function
πŸ‘‰Tutorial-8:  Reverse a String in Python

Python Strings: In Python, strings are objects that can be easily manipulated using various built-in methods. Python strings can be Python Strings created simply by enclosing text within double quotes. For example:

python

var = “Hello World!”

Python doesn’t have a dedicated character type; instead, characters are treated as strings of length one. You can access specific characters using square brackets.

python

var1 = “Software Moji Moji!”

var2 = “Software Testing”

print(“var1[0]:”, var1[0])  # Output: S

print(“var2[1:5]:”, var2[1:5])  # Output: oftw


Python provides multiple operators to perform string manipulations.

OperatorDescriptionExample
[]Slice a string to retrieve a character by indexx[1] β†’ “o”
[ : ]Range slice to retrieve characters within a specified rangex[1:3] β†’ “of”
inMembership operator to check if a substring exists in a string“Moji” in x β†’ True
not inMembership operator to check if a substring does not exist in a string“Test” not in x β†’ True
+Concatenates two strings“Python” + ” Moji” β†’ “Python Moji”
*Repeats the string specified number of times“Python”*2 β†’ “PythonPython”

The replace() method returns a copy of the string with specified values replaced.

python

oldstring = ‘I like Python Programming’

newstring = oldstring.replace(‘like’, ‘love’)

print(newstring)


I love Python Programming


  • upper() – Converts all characters to uppercase.
  • lower() – Converts all characters to lowercase.
  • capitalize() – Capitalizes the first character.

python

string = “python at software moji”

print(string.upper())        # Output: PYTHON AT SOFTWARE MOJI

print(string.capitalize())   # Output: Python at software moji

print(string.lower())        # Output: python at software moji


The join() method concatenates elements of a string or iterable by adding a specified separator.

python

print(“:”.join(“Python”))


P:y:t:h:o:n


The reversed() function can be used to reverse a string.

python

string = “12345”

print(”.join(reversed(string)))


54321


The split() method divides a string into a list of substrings based on a delimiter.

python

word = “Software Moji Moji career Software Moji Moji”

print(word.split(‘ ‘))  # Split by spaces

print(word.split(‘r’))  # Split by the letter ‘r’

css

[‘Software’, ‘Moji’, ‘Moji’, ‘career’, ‘Software’, ‘Moji’, ‘Moji’]

[‘Softwa’, ‘e Moji Moji ca’, ‘ee’, ‘ Softwa’, ‘e Moji Moji’]


  • Strings are immutable in Python. Methods like replace() return a new string instead of modifying the original one.
  • To update a string with replace(), reassign the result:

python

x = “Software Moji Moji”

x = x.replace(“Software Moji Moji”, “Python”)

print(x)  # Output: Python


Python offers powerful string manipulation capabilities using methods like replace(), join(), split(), reverse(), upper(), and lower(). Mastering these techniques will improve your efficiency in handling text data and writing cleaner code.

For advanced concepts, explore Python’s .format() function and additional string formatting methods.

Mutable vs Immutable Objects in Python

Download Python

Leave a Comment

Index