String Functions

String Functions


len(str)

Returns the length (no of characters) of the string.

Example:

>>> A="Corporation"
>>> print(len(A))

Output:

11


capitalize( )

 Used to capitalize the first character of the string

Example:

>>> city="chennai"
>>> print(city.capitalize())

Output:

Chennai


center(width, fillchar) 

Returns a string with the original string centered to a total of width columns and filled with fillchar in columns that do not have characters

Example:

>>> str1="Welcome"
>>> print(str1.center(15,'*') )

Output:

****Welcome****


find(sub[, start[, end]])

The function is used to search the first occurrence of the sub string in the given string. It returns the index at which the substring starts. It returns -1 if the substring does not occur in the string.

Example:

>>>str1=’mammals’
>>>str1.find(‘ma’)

Output:

0

On omitting the start parameters, the function starts the search from the beginning.

Example:

>>>str1.find(‘ma’,2)

Output:

3

Example:

>>>str1.find(‘ma’,2,4)

Output:

-1

Displays -1 because the substring could not be found between the index 2 and 4-1.


isalnum( )

Returns True if the string contains only letters and digit. It returns False. If the string contains any special character like _, @, #, *, etc.

Example;

>>>str1=’Save Earth’
>>>str1.isalnum()

Output:

False

The function returns False as space is an alphanumeric character.

Example:

>>>’Save1Earth’.isalnum()

Output:

True


isalpha( )

Returns True if the string contains only letters. Otherwise return False.

Example:

>>>’Click123’.isalpha()

Output:

False

Example;

>>>’python’.isalpha( )

Output:

True


isdigit( )

Returns True if the string contains only numbers. Otherwise it returns False.

Example:

>>> str1=’Save Earth’
>>>print(str1.isdigit( ))

Output:

False


lower( )

Returns the exact copy of the string with all the letters in lowercase.

Example:

>>>str1=’SAVE EARTH’
>>>print(str1.lower())

Output:

save earth


islower( )

Returns True if the string is in lowercase.

Example:

>>> str1=’welcome’

>>>print (str1.islower( ))

Output:

True


isupper( )

Returns True if the string is in uppercase.

Example:

>>> str1=’welcome’
>>>print (str1.isupper( ))

Output:

False


upper( )

Returns the exact copy of the string with all letters in uppercase.

Example:

>>> str1=’welcome’

>>>print (str.upper( ))

Output:

WELCOME


title( )

Returns a string in title case

Example:

>>> str1='education department'
>>> print(str1.title())

Output:

Education Department


swapcase( )

It will change case of every character to its opposite case vice-versa.

Example:

>>> str1="tAmiL NaDu"
>>> print(str1.swapcase())

Output:

TaMIl nAdU

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad