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 will be the output of the following Python statement?

>>>"a"+"bc"


  1. a
  2. bc
  3. bca
  4. abc
Qus. 2 :

What will be the output of the following Python statement?

>>>"abcd"[2:]


  1. a
  2. ab
  3. cd
  4. dc
Qus. 3 : What arithmetic operators cannot be used with strings?

  1. +
  2. *
  3. All of the mentioned
Qus. 4 :

What will be the output of the following Python code?

  1. >>>str1="helloworld"
  2. >>>str1[::-1]


  1. dlrowolleh
  2. hello
  3. world
  4. helloworld
Qus. 5 :

What will be the output of the following Python code?

  1. >>>example = "snow world"
  2. >>>example[3] = 's'
  3. >>>print example


  1. snow
  2. snow world
  3. Error
  4. snos world
Qus. 6 :

What will be the output of the following Python code?

  1. >>>example = "helle"
  2. >>>example.find("e")


  1. Error
  2. -1
  3. 1
  4. 0
Qus. 7 :

What will be the output of the following Python statement?

>>>chr(ord('A'))


  1. A
  2. B
  3. a
  4. Error
Qus. 8 :

What will be the output of the following Python statement?

>>>print(chr(ord('b')+1))


  1. a
  2. b
  3. c
  4. A
Qus. 9 : Which of the following statement prints helloexample test.txt?

  1. print(“helloexample est.txt”)
  2. print(“helloexample test.txt”)
  3. print(“hello”example”test.txt”)
  4. print(“hello”example”est.txt”)
Qus. 10 : What will be the output of the “hello” +1+2+3?

  1. hello123
  2. hello
  3. Error
  4. hello6
Qus. 11 : What will be displayed by print(ord(‘b’) – ord(‘a’))?

  1. 0
  2. 1
  3. -1
  4. 2
Qus. 12 : which function do you use to read a string?

  1. input(“Enter a string”)
  2. eval(input(“Enter a string”))
  3. enter(“Enter a string”)
  4. eval(enter(“Enter a string”))
Qus. 13 :

What will be the output of the following Python code?

print("abcdef".center())


  1. cd
  2. abcdef
  3. error
  4. none of the mentioned
Qus. 14 :

What will be the output of the following Python code?

print("abcdef".center(7, '1'))


  1. 1abcdef
  2. abcdef1
  3. abcdef
  4. error
Qus. 15 :

What will be the output of the following Python code?

print("abcdef".center(10, '12'))


  1. 12abcdef12
  2. abcdef1212
  3. 1212abcdef
  4. error
Qus. 16 :

What will be the output of the following Python code?

print("xyyzxyzxzxyy".count('yy', 1))


  1. 2
  2. 0
  3. 1
  4. none of the mentioned
Qus. 17 :

What will be the output of the following Python code?

print("xyyzxyzxzxyy".count('xyy', 2, 11))


  1. 2
  2. 0
  3. 1
  4. error
Qus. 18 : What is the default value of encoding in encode()?

  1. ascii
  2. qwerty
  3. utf-8
  4. utf-16
Qus. 19 :

What will be the output of the following Python code?

print("xyyzxyzxzxyy".endswith("xyy"))


  1. 1
  2. True
  3. 3
  4. 2
Qus. 20 :

What will be the output of the following Python code?

print("xyyzxyzxzxyy".endswith("xyy", 0, 2))


  1. 0
  2. 1
  3. True
  4. False
Qus. 21 :

What will be the output of the following Python code?

print("ab\tcd\tef".expandtabs())


  1. ab  cd  ef
  2. abcdef
  3. ab cd ef
  4. ab cd ef
Qus. 22 :

What will be the output of the following Python code?

print("ab\tcd\tef".expandtabs(4))


  1. ab  cd  ef
  2. abcdef
  3. ab cd ef
  4. ab cd ef
Qus. 23 :

What will be the output of the following Python code?

print("ab\tcd\tef".expandtabs('+'))


  1. ab+cd+ef
  2. ab++++++++cd++++++++ef
  3. ab cd ef
  4. none of the mentioned
Qus. 24 :

What will be the output of the following Python code?

print("abcdef".find("cd") == "cd" in "abcdef")


  1. True
  2. False
  3. Error
  4. None of the mentioned
Qus. 25 :

What will be the output of the following Python code?

print("abcdef".find("cd"))


  1. True
  2. 2
  3. 3
  4. None of the mentioned
Qus. 26 :

What will be the output of the following Python code?

print("Hello {0} and {1}".format('foo', 'bin'))


  1. Hello foo and bin
  2. Hello {0} and {1} foo bin
  3. Error
  4. Hello 0 and 1
Qus. 27 :

What will be the output of the following Python code?

print("Hello {1} and {0}".format('bin', 'foo'))


  1. Hello foo and bin
  2. Hello bin and foo
  3. Error
  4. None of the mentioned
Qus. 28 :

What will be the output of the following Python code?

print("Hello {} and {}".format('foo', 'bin'))


  1. Hello foo and bin
  2. Hello {} and {}
  3. Error
  4. Hello and
Qus. 29 :

What will be the output of the following Python code?

print("Hello {name1} and {name2}".format(name1='foo', name2='bin'))


  1. Hello foo and bin
  2. Hello {name1} and {name2}
  3. Error
  4. Hello and
Qus. 30 :

What will be the output of the following Python code?

print("Hello {0!r} and {0!s}".format('foo', 'bin'))


  1. Hello foo and foo
  2. Hello ‘foo’ and foo
  3. Hello foo and ‘bin’
  4. Error
Qus. 31 :

What will be the output of the following Python code?

print("Hello {0} and {1}".format(('foo', 'bin')))


  1. Hello foo and bin
  2. Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)
  3. Error
  4. None of the mentioned
Qus. 32 :

What will be the output of the following Python code?

print('ab,12'.isalnum())


  1. True
  2. False
  3. None
  4. Error
Qus. 33 :

What will be the output of the following Python code?

print('ab'.isalpha())


  1. True
  2. False
  3. None
  4. Error
Qus. 34 :

What will be the output of the following Python code?

print('a B'.isalpha())


  1. True
  2. False
  3. None
  4. Error
Qus. 35 :

What will be the output of the following Python code snippet?

print('0xa'.isdigit())


  1. True
  2. False
  3. None
  4. Error
Qus. 36 :

What will be the output of the following Python code snippet?

print(''.isdigit())


  1. True
  2. False
  3. None
  4. Error
Qus. 37 :

What will be the output of the following Python code snippet?

print('my_string'.isidentifier())


  1. True
  2. False
  3. None
  4. Error
Qus. 38 :

What will be the output of the following Python code snippet?

print('__foo__'.isidentifier())


  1. True
  2. False
  3. None
  4. Error
Qus. 39 :

What will be the output of the following Python code snippet?

print('for'.isidentifier())


  1. True
  2. False
  3. None
  4. Error
Qus. 40 :

What will be the output of the following Python code snippet?

print('abc'.islower())


  1. True
  2. False
  3. None
  4. Error
Qus. 41 :

What will be the output of the following Python code snippet?

print('11'.isnumeric())


  1. True
  2. False
  3. None
  4. Error
Qus. 42 :

What will be the output of the following Python code snippet?

print('1.1'.isnumeric())


  1. True
  2. False
  3. None
  4. Error
Qus. 43 :

What will be the output of the following Python code snippet?

print('1@ a'.isprintable())


  1. True
  2. False
  3. None
  4. Error
Qus. 44 :

What will be the output of the following Python code snippet?

print(''''''.isspace())


  1. True
  2. False
  3. None
  4. Error
Qus. 45 :

What will be the output of the following Python code snippet?

print('\t'.isspace())


  1. True
  2. False
  3. None
  4. Error
Qus. 46 :

What will be the output of the following Python code snippet?

print('HelloWorld'.istitle())


  1. True
  2. False
  3. None
  4. Error
Qus. 47 :

What will be the output of the following Python code snippet?

print('Hello World'.istitle())


  1. True
  2. False
  3. None
  4. Error
Qus. 48 :

What will be the output of the following Python code?

print('xyyzxxyxyy'.lstrip('xyy'))


  1. error
  2. zxxyxyy
  3. z
  4. zxxy
Qus. 49 :

What will be the output of the following Python code?

print('xyxxyyzxxy'.lstrip('xyy'))


  1. zxxy
  2. xyxxyyzxxy
  3. xyxzxxy
  4. none of the mentioned
Qus. 50 :

What will be the output of the following Python code?

print('cba'.maketrans('abc', '123'))


  1. {97: 49, 98: 50, 99: 51}
  2. {65: 49, 66: 50, 67: 51}
  3. 321
  4. 123
Qus. 51 :

What will be the output of the following Python code?

print('a'.maketrans('ABC', '123'))


  1. {97: 49, 98: 50, 99: 51}
  2. {65: 49, 66: 50, 67: 51}
  3. {97: 49}
  4. 1
Qus. 52 :

What will be the output of the following Python code?

print('abcdef'.partition('cd'))


  1. (‘ab’, ‘ef’)
  2. (‘abef’)
  3. (‘ab’, ‘cd’, ‘ef’)
  4. 2
Qus. 53 :

What will be the output of the following Python code?

print('abcdefcdgh'.partition('cd'))


  1. (‘ab’, ‘cd’, ‘ef’, ‘cd’, ‘gh’)
  2. (‘ab’, ‘cd’, ‘efcdgh’)
  3. (‘abcdef’, ‘cd’, ‘gh’)
  4. error
Qus. 54 :

What will be the output of the following Python code snippet?

print('cd'.partition('cd'))


  1. (‘cd’)
  2. (”)
  3. (‘cd’, ”, ”)
  4. (”, ‘cd’, ”)
Qus. 55 :

What will be the output of the following Python code snippet?

print('abef'.partition('cd'))


  1. (‘abef’)
  2. (‘abef’, ‘cd’, ”)
  3. (‘abef’, ”, ”)
  4. error
Qus. 56 :

What will be the output of the following Python code snippet?

print('abcdef12'.replace('cd', '12'))


  1. ab12ef12
  2. abcdef12
  3. ab12efcd
  4. none of the mentioned
Qus. 57 :

What will be the output of the following Python code snippet?

print('abcefd'.replace('cd', '12'))


  1. ab1ef2
  2. abcefd
  3. ab1efd
  4. ab12ed2
Qus. 58 :

What will be the output of the following Python code snippet?

print('xyyxyyxyxyxxy'.replace('xy', '12', 100))


  1. xyyxyyxyxyxxy
  2. 12y12y1212x12
  3. none of the mentioned
  4. error
Qus. 59 :

What will be the output of the following Python code snippet?

print('abcdefcdghcd'.split('cd'))


  1. [‘ab’, ‘ef’, ‘gh’]
  2. [‘ab’, ‘ef’, ‘gh’, ”]
  3. (‘ab’, ‘ef’, ‘gh’)
  4. (‘ab’, ‘ef’, ‘gh’, ”)
Qus. 60 :

What will be the output of the following Python code snippet?

print('abcdefcdghcd'.split('cd', 0))


  1. [‘abcdefcdghcd’]
  2. ‘abcdefcdghcd’
  3. error
  4. none of the mentioned
Qus. 61 :

What will be the output of the following Python code snippet?

print('abcdefcdghcd'.split('cd', -1))


  1. [‘ab’, ‘ef’, ‘gh’]
  2. [‘ab’, ‘ef’, ‘gh’, ”]
  3. (‘ab’, ‘ef’, ‘gh’)
  4. (‘ab’, ‘ef’, ‘gh’, ”)
Qus. 62 :

What will be the output of the following Python code snippet?

print('abcdefcdghcd'.split('cd', 2))


  1. [‘ab’, ‘ef’, ‘ghcd’]
  2. [‘ab’, ‘efcdghcd’]
  3. [‘abcdef’, ‘ghcd’]
  4. none of the mentioned
Qus. 63 :

What will be the output of the following Python code snippet?

print('ab\ncd\nef'.splitlines())


  1. [‘ab’, ‘cd’, ‘ef’]
  2. [‘ab ’, ‘cd ’, ‘ef ’]
  3. [‘ab ’, ‘cd ’, ‘ef’]
  4. [‘ab’, ‘cd’, ‘ef ’]
Qus. 64 :

What will be the output of the following Python code snippet?

print('Ab!2'.swapcase())


  1. AB!@
  2. ab12
  3. aB!2
  4. aB1@
Qus. 65 :

What will be the output of the following Python code snippet?

print('ab cd ef'.title())


  1. Ab cd ef
  2. Ab cd eF
  3. Ab Cd Ef
  4. None of the mentioned
Qus. 66 :

What will be the output of the following Python code snippet?

print('ab cd-ef'.title())


  1. Ab cd-ef
  2. Ab Cd-ef
  3. Ab Cd-Ef
  4. None of the mentioned
Qus. 67 :

What will be the output of the following Python code snippet?

print('abcd'.translate('a'.maketrans('abc', 'bcd')))


  1. bcde
  2. abcd
  3. error
  4. bcdd
Qus. 68 :

What will be the output of the following Python code snippet?

print('abcd'.translate({'a': '1', 'b': '2', 'c': '3', 'd': '4'}))


  1. abcd
  2. 1234
  3. error
  4. none 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


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