String means group of character , the strings are familier since we already used in many programming language . But the python supported strings provides lot of inbuilt functions for the efficient manipulation of the strings.In this post we elaborate the string methods in python.
The python string supports indexing and slicing of the strings. Indexing means ,python assigns a integer value starts from 0 .
For Ex: Hello world is viewed as
test = ‘H E L L O W O R L D’
0 1 2 3 4 5 6 7 8 9 10 11 .
We can access each character by specifying the index like
test ——> H
Methods supported by Strings.
Useful String Methods
- upper() : Convert all strings to uppercase
- lower() : Converts all strings to lowercase.
- isupper() : Check whether all characters are upper case or not
- islower() : Check whether all characters are lower case or not and returns a boolean value .
The example are as follows:
The isX string Methods
- isalpha(): Return true if the string contains only alphabets.
- isalnum():Return true if the string contains only alphabets or numerics.
- isdecimal(): Return true if the string contains only numbers.
- isspace() : Return true if the string contains only spaces,tabs etc.
- istitle(): Return true if the string contains only words starting with capital letter followed by small letter.
- startswith() and endswith():Return true if the string value they are called on begins or ends (respectively) with the string passed to the method.
The examples for all the above methods are given in the following code snippet:
join() and split() methods
The join methods to join the list of strings into a single list wheres the split() methods splits the string into a list of strings . The following example illustrate this.
>> ‘, ‘ . join([‘cats’, ‘rats’, ‘bats’]). –> ‘cats, rats, bats’
>>> ‘ ‘ . join([‘My’, ‘name’, ‘is’, ‘Simon’]) —> ‘My name is Simon’
>>> ‘ABC’ . join([‘My’, ‘name’, ‘is’, ‘Simon’])–>’MyABCnameABCisABCSimon’.
in the above example ,join method combines the strings with character specified in quotes .
In the last example , ABC is joined with each of the strings.
split() works exactly complementary to this .
>>> ‘My name is Raj’ . split()
[ ‘My’ , ‘name’ , ‘is’ , ‘Raj’ ].
Justifying Text with rjust(), ljust(), and center() methods
The rjust() and ljust() string methods return a padded version of the string they are called on, with spaces inserted to justify the text. The first argument to both methods is an integer length for the justified string.
‘ Hello ‘
center () will place the string at the middle just as seen in MS word.
These methods takes second optional parameter to insert any character for the padded space.
Removing Whitespace with strip(), rstrip(), and lstrip()
Python provides method to remove the space like complementary to the just methods. Similar to just() ,it has lstrip(), rstrip() and strip().
lstrip() : remove the left side space.
rstrip() : Remove the right side space.
strip() : Remove the spaces both left and right side.
I discussed the different methods supported by python for handling strings ,in future I will update more information to this ,please give your comment about this article.