0% found this document useful (0 votes)
8 views

String Methods in Python

Uploaded by

lambiadila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

String Methods in Python

Uploaded by

lambiadila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

String

Meth ds
in Python
join( )
Converts the elements of an iterable
into a string.

feel = ["Sad", "Happy", "Angry"]


string = ' '.join(feel)
print(string)

Output
"Sad Happy Angry"
split( )
Splits the string at the specified separator,
and returns a list.

person = "Jack,John,Jerry"
separator = ','
print(person.split(separator))

Output
['Jack', 'John', 'Jerry']
title( )
Converts the first character of each word
to upper case.

string = "medical medal mars"


print(string.title())

Output
"Medical Medal Mars"
replace( )
Returns a string where a specified value is
replaced with a specified value.

intro = "I am a programmer"


new = intro.replace("programmer", "doctor")
print(new)

Output
"I am a doctor"
upper( )

Converts a string into upper case.

animal = "lion"
print(animal.upper())

Output
"LION"
lower( )

Converts a string into lower case.

animal = "SHEEP"
print(animal.lower())

Output
"sheep"
casefold( )

Converts string into lower case.

job = "PROGRAMMER"
print(job.casefold())

Output
"programmer"
capitalize( )

Converts the first character to upper case.

capital = "tokyo"
print(capital.capitalize())

Output
"Tokyo"
swapcase( )
Lower case becomes upper case and vice
versa.

capital = "perSON"
print(capital.swapcase())

Output
"PERson"
count( )
Returns the number of times a specified
value occurs in a string.

string = "Python is a programming language"


count = string.count('a')
print(count)

Output
4
index( )

Searches the string for a specified value


and returns the position.

string = "Python is a programming language"


index = string.index('a')
print(index)

Output
10
rindex( )
Searches the string for a specified value
and returns the last position.

string = "Python is a programming language"


index = string.rindex('a')
print(index)

Output
29
find( )

Searches the string for a specified value


and returns the position.

string = "Python is a programming language"


pos = string.find('i')
print(pos)

Output
7
startswith( )
Returns True if the string starts with the
specified value.

string = "Hello World"


print(string.startswith('H'))

Output
True
endswith( )
Returns True if the string ends with the
specified value.

string = "Hello World"


print(string.endswith('s'))

Output
False
format( )

Formats specified values in a string.

string = "{0} is a programming language"


print(string.format("Python"))

Output
"Python is a programming language"
isalnum( )

Returns True if all characters in the string


are alphanumeric.

string = "Python3"
print(string.isalnum())

Output
True
isalpha( )

Returns True if all characters in the string


are in the alphabet.

string = "Python"
print(string.isalpha())

Output
True
isnumeric( )

Returns True if all characters in the string


are numeric.

string = "2023"
print(string.isnumeric())

Output
True
strip( )

Returns a trimmed version of the string.

string = " Python is a programming language "


strip = string.strip()
print(strip)

Output
"Python is a programming language"
center( )

Returns a centered string.

job = "doctor"
print(job.center(15))

Output
" doctor "
rjust( )

Returns a right justified version of the string.

string = "Python"
rightJustify = string.rjust(20)
print(rightJustify)

Output
" Python"
ljust( )

Returns a left justified version of the string.

string = "Python"
leftJustify = string.ljust(20)
print(leftJustify)

Output
"Python "

You might also like