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

Qus. 1 : What arithmetic operators cannot be used with strings?

  1. +
  2. *
  3. -
  4. All of the mentioned

Programs

python program input string and prints its characters in different lines two characters per line

View Solution


python to print the number of occurrences of a sub string into a line

View Solution


python program to check the given string is palindrome or not

View Solution


python program that inputs a string and then prints it equal to number of times its length

View Solution


python program to form a word by joining first character of each word

View Solution


python program to reverse order of its words in given sentence

View Solution


python program to find the longest word present in the sentence along with its length

View Solution


python program to find the frequency of vowels in each word

View Solution


python program to input a word and display the pattern as shown below

View Solution


Latest Current Affairs 2025 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad