
π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: Replace, Join, Split, Reverse, Uppercase & Lowercase
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 Strings Accessing Values in Strings
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.
Example Code:
python
var1 = “Software Moji Moji!”
var2 = “Software Testing”
print(“var1[0]:”, var1[0]) # Output: S
print(“var2[1:5]:”, var2[1:5]) # Output: oftw
Various String Operators in Python
Python provides multiple operators to perform string manipulations.
Operator | Description | Example |
[] | Slice a string to retrieve a character by index | x[1] β “o” |
[ : ] | Range slice to retrieve characters within a specified range | x[1:3] β “of” |
in | Membership operator to check if a substring exists in a string | “Moji” in x β True |
not in | Membership 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” |
Python String Methods with Examples
1. Python replace() Method
The replace() method returns a copy of the string with specified values replaced.
Example Code:
python
oldstring = ‘I like Python Programming’
newstring = oldstring.replace(‘like’, ‘love’)
print(newstring)
Output:
I love Python Programming
2. Changing Uppercase and Lowercase
- upper() – Converts all characters to uppercase.
- lower() – Converts all characters to lowercase.
- capitalize() – Capitalizes the first character.
Example Code:
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
3. Python join() Method
The join() method concatenates elements of a string or iterable by adding a specified separator.
Example Code:
python
print(“:”.join(“Python”))
Output:
P:y:t:h:o:n
4. Reversing a String in Python
The reversed() function can be used to reverse a string.
Example Code:
python
string = “12345”
print(”.join(reversed(string)))
Output:
54321
5. Python split() Method
The split() method divides a string into a list of substrings based on a delimiter.
Example Code:
python
word = “Software Moji Moji career Software Moji Moji”
print(word.split(‘ ‘)) # Split by spaces
print(word.split(‘r’)) # Split by the letter ‘r’
Output:
css
[‘Software’, ‘Moji’, ‘Moji’, ‘career’, ‘Software’, ‘Moji’, ‘Moji’]
[‘Softwa’, ‘e Moji Moji ca’, ‘ee’, ‘ Softwa’, ‘e Moji Moji’]
Important Note on Python Strings
- 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:
Example Code:
python
x = “Software Moji Moji”
x = x.replace(“Software Moji Moji”, “Python”)
print(x) # Output: Python
Summary
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.