String Manipulation Techniques
If you want to manipulate a string and transform its processing capabilities as needed, then Python allows you some techniques to manipulate a string:
Reverse a String
If you want to reverse a string then you can do that by following the method below:
Example:
# Create a string
Text = "Intellipaat"
print(Text[::-1]) # Output: taapilletnI
Split a String
If you want to extract all the words of a string in a list format, then you can do that by using the split() method.
Example:
# Create a string
Text = "Learn With Intellipaat"
# Splitting the string
print(Text.split(" ")) # Output: ['Learn', 'With', 'Intellipaat']
Replacing Substrings
If you want to replace parts of a string with another substring, then you can do that by using the replace() method.
Example:
# Create a string
Text = "Learn With Intellipaat"
# Replacing a substring
print(Text.replace("Learn", "Grow")) # Output: Grow With Intellipaat
Changing Case
If you want to convert a string to uppercase, lowercase or some other format, then Python provides you with some methods to do the same.
Example:
# Create a string
Text = "learn with intellipaat"
# Changing to uppercase
print(Text.upper()) # Output: LEARN WITH INTELLIPAAT
# Changing to lowercase
print(Text.lower()) # Output: learn with intellipaat
# Capitalizing the first character of every word
print(Text.title()) # Output: Learn With Intellipaat
# Capitalizing the first character only
print(Text.capitalize()) # Output: Learn with intellipaat
Stripping Characters
If you want to remove characters from the beginning or end of a string, then you can use some methods like strip(), lstrip(), or rstrip().
Example:
# Create a string
Text = "$$$Intellipaat$$$"
# Stripping all '$' characters from both ends
print(Text.strip("$")) # Output: Intellipaat
# Stripping '$' characters from the left
print(Text.lstrip("$")) # Output: Intellipaat$$$
# Stripping '$' characters from the right
print(Text.rstrip("$")) # Output: $$$Intellipaat
String Formatting
If you want to format a string by combining variables, expressions, or data values in a structured way to make it more readable and meaningful, Python provides multiple methods to do the same. Following are some of these methods:
By using this operator, you can easily concatenate two or more strings and variables.
# Create a string
Text = "Intellipaat"
# Concatenation of strings
print("Learn " + "With " + Text)
In this method, you can use a placeholder wherever you want to insert a string or a variable. Placeholders like %s(for strings), %d(for integers), and %f(for float) are used.
# Create a string
Text1 = "Intellipaat"
Text2 = "Python"
number = 1
# Concatenation of strings
print("%s provides No. %d %s Course" % (Text1, number, Text2))
# Output: Intellipaat provides No. 1 Python Course
Using format() Method
Similar to the % operator, the format() method works in the same way, but it is easier to work with and provides more flexibility to developers, as no placeholders are required here.
# Create a string
Text1 = "Intellipaat"
Text2 = "Python"
number = 1
# Concatenation of strings
print("{} provides No. {} {} Course".format(Text1, number, Text2))
# Output: Intellipaat provides No. 1 Python Course
This is the most modern and efficient method of formatting strings.
# Create a string
Text1 = "Intellipaat"
Text2 = "Python"
number = 1
# Concatenation of strings
print(f"{Text1} provides No. {Text2} {number} Course")
# Output: Intellipaat provides No. 1 Python Course
Slicing of Strings
If you want to extract a segment of a string, then you can do that by defining a range of indices. As you know, strings in Python are sequences of characters that start with index 0, then 1, and so on.
Example:
# Create a string
Text = "Intellipaat"
# Slicing of strings
sliced_text = Text[0:5]
# Printing the sliced string
print(sliced_text) # Output: Intel
The above example illustrates a simple slicing method. Following are some more methods to perform string slicing:
- Omitting the Start or End Index
If the starting or ending index is not provided, then Python itself sets it to the lower and upper limit respectively.
Example:
# Create a string
Text = "Intellipaat"
# Slicing of strings
print(Text[:8]) # Output: Intellip
print(Text[4:]) # Output: llipaat
If you want to extract characters in an interval, then you can define the step as a parameter.
Example:
# Create a string
Text = "Intellipaat"
# Slicing of strings after an interval
print(Text[::2]) # Output: Itliat
- Negative Slicing of Strings
If you pass a negative index as a parameter, then Python will count that index from the end.
Example:
# Create a string
Text = "Intellipaat"
# Negative Slicing of strings
print(Text[-5:]) # Output: ipaat
If you pass -1 as a step parameter, then it will reverse the string.
Example:
# Create a string
Text = "Intellipaat"
# Reversing a string
print(Text[::-1]) # Output: taapilletnI
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
Python provides a lot of methods and operators for string operations. These methods help developers in manipulating text data efficiently. With operations like slicing, formatting, and methods like join(), strip(), split(), and many more, you can process complex data easily.
If you want to explore more in this domain, you can enroll in Intellipaat’s Python Course and Give your career the right choice.