String Processing
String processing is a very broad topic, but in Python, there are many
built-in methods and functions that make it easy to work with
strings.
Python String Methods:
- String Slicing
- String Stripping
- String Splitting
- String Reversing
- String length
- Uppercase and lowercase
- Replacing Substring
- Palindrome check
String Slicing:
In Python, the process of extracting a portion of a string by specifying a
range of indices is known as string slicing.
Program:
# Define a string variable
string = "Hello, world!"
# Extract the first five characters of the string
substring = string[0:5]
print("Substring:", substring)
# Extract the last six characters of the string
substring = string[-6:]
print("Substring:", substring)
# Extract every other character of the string
substring = string[::2]
print("Substring:", substring)
Output:
Substring: Hello
Substring: world!
Substring: Hlo ol!
String Stripping:
In Python, the process of stripping a string of its leading and/or trailing
characters is known as string stripping.
Program:
# Define a string variable with leading/trailing whitespace
string = "
Hello, world! "
# Strip leading/trailing whitespace from the string
stripped_string = string.strip()
# Print the stripped string
print("Stripped string:", stripped_string)
Output:
Stripped string: Hello, world!
String Splitting:
The split() method in Python allows you to divide a string into a list of
substrings. The split() method splits the original string wherever it finds
a separator, taking as its argument a separator string.
Program 1:
string = "Hello World"
words = string.split(" ")
print(words)
Output:
['Hello', 'World']
Program 2:
string = "apple,banana,orange"
fruits = string.split(",")
print(fruits)
Output:
['apple', 'banana', 'orange']
String Reversing:
There is no built-in function for reversing the given string in Python By cutting that number of steps backward, -1, it is done.
Program:
string = "Hello World"
reversed_string = string[::-1]
print(reversed_string)
Output:
dlroW olleH
String Length:
The length of the given string can be determined using the len() function.
Program:
# string_length.py
string = "Hello World" length =
len(string)
print(length)
Output:
11
Uppercase And Lowercase:
The upper() function encases the given string in upper case, and the
lower() function encases it in lower case.
Program:
string = input("Enter a string:
")
# convert to uppercase and print
uppercase_string = string.upper()
print("Uppercase string:
", uppercase_string)
# convert to lowercase and print
lowercase_string = string.lower()
print("Lowercase string:
", lowercase_string)
Output:
Enter a string: Hello World
Uppercase string: HELLO WORLD
Lowercase string: hello world
Replacing Substring:
Using the replace() method, you can replace a substring within a string in
Python. Two arguments are accepted by the replace() method: the replacement
substring and the substring that will be replaced.
Program:
string = "Hello World"
new_string = string.replace("World",
"Python")
print(new_string)
Output:
Hello Python
Palindrome Check:
By comparing a string to its opposite in Python, you can determine whether
or not it is a palindrome. A word, phrase, number, or other sequence of
characters that reads the same way forward and backward is known as a
palindrome.
Program:
string = input("Enter a string:
")
reverse_string = string[::-1]
if string == reverse_string:
print(string, "is a palindrome")
else:
print(string, "is not a palindrome")
Output 1:
Enter a string: racecar
racecar is a palindrome
Output 2:
Enter a string: python
python is not a palindrome
More topic in Python