It is the process of python built-in string methods and formatting operations. In python, the string can be defined between the single and double quotations.
x = 'string here 1'
y = "string here 1"
x == y
it gives "true" as an output.
String Manipulation in Python
In python string handle through many ways like:
upper() - Used to change a string into the uppercase letter
lower() - Used to change a string into the lowercase letter
capitalize() - show string with the first letter as a capital
title() - It works as a capitalize()
swapcase() - every string word look as first and the last word is capital
Let we will learn these string manipulation functions using python which is given below:
Adding and removing spaces - Formatting of string
In the python strip() method used to remove space, it strips whitespace from the beginning and end of the line.
Let suppose string is -
>>> string = " Welcome to the Codersarts "
>>> string.strip()
Output result shows without any space in string.
Another way to remove space from left and right side of the string is like -
rstrip() or - remove space from the right side of a string
lstrip() - remove space from the left side of the string
There are many other formattings of strings
center() - Center the string within given space
ljust() and - will left-justify the string within spaces of a given length.
rjust() - right-justify the string within spaces of a given length
zfill() - which is a special method to right-pad a string with zeros.
Finding and replacing substrings
This is the main common feature that is used to find and replacing substrings from a string in python.
find()/rfind(),
index()/rindex(), and
replace()
These built-in methods are the best for finding and replacing substrings.
Now we discuss rfind() and rindex() methods- These work same as above but searching from the end, not beginning and
rindex()
rfind()
startswith() and methods endswith() - These used in python for the special case of checking for a substring at the beginning or end of a string.
replace() - Use to replace a given substring with a new string.
Splitting and partitioning strings
If you want to find substring and then split the string based on its location,
the partitions() and/or
split() methods used.
The split() method is more useful. The default is to split on any whitespace, returning a list of the individual words in a string:
For Ex -
line = "This is codersarts blog"
line.split()
It output shows as:
['This', 'is', 'codersarts', 'blog']
Another related method is splitlines(), which splits on newline characters.
>>> string = """this
>>> is codersarts
>>> blog"""
string.splitlines()
The outcome is like that - ['this', 'is codersarts', 'blog']
Format Strings
Used to manipulate strings into desired formats.
string representations can always be found using the str() function.
let we learn it as -
>>> number= 2.458
>>> str(number)
The outcome is like that - '2.458', it is an string formate of number 2.458
A more flexible way to do this is to use format strings, which are strings with special markers (noted by curly braces)
Let we check this:
>>> print("The value of number is {}".format(number))
Output:
'The value of the number is 2.458'
Another more examples to formatting string are like this:
>>> print("My name is: {0}. and village is: {1}.".format('Naveen kumar', 'Etah'))
'My name is: Naveen kumar. and village is: Etah.'
>>> print("First letter: {first}. Last letter: {last}.".format(last='N', first='R'))
'First letter: N. Last letter: R.'
Apply on digit:
>>> number = 3.142
>>> print("number= {0:.2f}".format(number))
'number= 3.14'
In this blog, we will cover the most useful string manipulation methods which are used in python to manipulate the string. It makes easy to perform the operation on strings.
If you like Codersarts blogs then please make comments in blow comments sections or give an idea if anything is missing so we can analyze our skill and provide the best ideas to us.
If you need any type of programming, coding or project help then visit Codersarts or contact here.
コメント