C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Python

Python string methods with Examples

String methods in Python: Here we will learn about predefined methods of String in Python with Examples and Explanations.
Submitted by Shubham Singh Rajawat, on August 05, 2017

Python has given many useful methods for strings, some of them we will cover in this article.

1. strip([chars])

will return a string in which all the characters specified have been stripped of from the beginning and the end of the string.

Here,
chars - characters to be removed from beginning or the end, by default space.

Example: #implementation of strip

string="includehelp is a portal to learn concepts"
print(string)
print(string.strip('incspt'))  #removes characters I, n, c, s, p, t  from beg and end
print(string.strip())    #removes spaces from beg and end

Output

includehelp is a portal to learn concepts
ludehelp is a portal to learn conce
includehelp is a portal to learn concepts

2. find(str,beg,end)

returns the position of the first occurrence of specified string between ‘beg’ and ‘end’ where beg is inclusive and end is exclusive. If the string is not found returns -1.

Here,
str - string to be searched
beg - starting index, by default 0
end - ending index, by default length of string

Example: #implementation of find

string="includehelp is a portal to learn concepts"
print(string.find("include",0,6))  #return -1 as 6 is exclusive
print(string.find("include",0,7))
print(string.find("por"))

Output

-1
0
17

3. rfind(str,beg,end)

returns the position of the last occurrence of the specified string. This function works similar to 'find()'.

4. split(str,limit)

As name specifies it splits the string from the specified delimeter 'str', returns a list.

Here,
str - this is any delimiter, by default space
limit - limits numbers to split

Example: #Implementation of split method

string="includehelp is a portal to learn concepts"
l=string.split()
print(l)
m=string.split('o')
print(m)
n=string.split('o',1)
print(n)

Output

['includehelp', 'is', 'a', 'portal', 'to', 'learn', 'concepts']
['includehelp is a p', 'rtal t', ' learn c', 'ncepts']
['includehelp is a p', 'rtal to learn concepts']

5. lower()

returns a string with all the letters in lowercase.

6. upper()

returns a string with all letters in uppercase.

7. title()

return a string by converting it into title case i.e. first letter of all the words will be uppercase.

8. capitalize()

return a string with only the first letter of the first word as uppercase.

9. startswith(prefix,beg,end)

returns true if the string starts with specified string ‘prefix’ else return false.

Here,
prefix - string to be checked
beg - starting index, by default 0
end - ending index ,optional, by default length of string

10. endswith(suffix,beg,end)

returns true if the string ends with specified string ‘suffix’ else return false.

Here,
suffix - string to be checked
beg - starting index, by default 0
end - ending index ,optional, by default length of string

Example: #program to implement above methods

string="Includehelp is a portal to Learn Concepts"
print(string.lower())
print(string.upper())
print(string.title())
print(string.capitalize())
print(string.endswith('rn',0,32))
print(string.startswith('rt',19))

Output

includehelp is a portal to learn concepts
INCLUDEHELP IS A PORTAL TO LEARN CONCEPTS
Includehelp Is A Portal To Learn Concepts
Includehelp is a portal to learn concepts
True
True








COMMENTS