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 statement?

>>>print('new' 'line')


  1. Error
  2. Output equivalent to print ‘newline’
  3. newline
  4. new line
Qus. 5 :

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. 6 : print(0xA + 0xB + 0xC):

  1. 0xA0xB0xC
  2. Error
  3. 0x22
  4. 33
Qus. 7 :

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. 8 :

What will be the output of the following Python code?

>>>max("what are you")


  1. error
  2. u
  3. t
  4. y
Qus. 9 :

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. 10 :

What will be the output of the following Python code?

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


  1. -1
  2. 4
  3. 3
  4. 1
Qus. 11 :

What will be the output of the following Python code?

  1. >>>example="helloworld"
  2. >>>example[::-1].startswith("d")


  1. dlrowolleh
  2. True
  3. -1
  4. None
Qus. 12 :

What will be the output of the following Python statement?

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


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

What will be the output of the following Python statement?

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


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

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

  1. hello123
  2. hello
  3. Error
  4. hello6
Qus. 16 :

What will be the output of the following Python statement?(python 3.xx)

  1. >>>print(format("Welcome", "10s"), end = '#')
  2. >>>print(format(111, "4d"), end = '#')
  3. >>>print(format(924.656, "3.2f"))


  1. Welcome# 111#924.66
  2. Welcome#111#924.66
  3. Welcome#111#.66
  4. Welcome # 111#924.66
Qus. 17 : What will be displayed by print(ord(‘b’) – ord(‘a’))?

  1. 0
  2. 1
  3. -1
  4. 2
Qus. 18 : 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. 19 :

What will be the output of the following Python code?

print("abc DEF".capitalize())


  1. abc def
  2. ABC DEF
  3. Abc def
  4. Abc Def
Qus. 20 :

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. 21 :

What will be the output of the following Python code?

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


  1. * abcdef *
  2. * abcdef *
  3. *abcdef *
  4. * abcdef*
Qus. 22 :

What will be the output of the following Python code?

print('*', "abcdef".center(7), '*', sep='')


  1. * abcdef *
  2. * abcdef *
  3. *abcdef *
  4. * abcdef*
Qus. 23 :

What will be the output of the following Python code?

print('*', "abcde".center(6), '*', sep='')


  1. * abcde *
  2. * abcde *
  3. *abcde *
  4. * abcde*
Qus. 24 :

What will be the output of the following Python code?

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


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

What will be the output of the following Python code?

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


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

What will be the output of the following Python code?

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


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

What will be the output of the following Python code?

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


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

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. 29 :

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. 30 :

What will be the output of the following Python code?

print("xyyzxyzxzxyy".count('xyy', -10, -1))


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

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

What will be the output of the following Python code?

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


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

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. 34 :

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. 35 :

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. 36 :

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. 37 :

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. 38 :

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. 39 :

What will be the output of the following Python code?

print("ccdcddcd".find("c"))


  1. 4
  2. 0
  3. Error
  4. True
Qus. 40 :

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. 41 :

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. 42 :

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. 43 :

What will be the output of the following Python code?

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


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

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. 45 :

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. 46 :

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. 47 :

What will be the output of the following Python code?

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


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

What will be the output of the following Python code?

print('ab'.isalpha())


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

What will be the output of the following Python code?

print('a B'.isalpha())


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

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

print('0xa'.isdigit())


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

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

print(''.isdigit())


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

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

print('my_string'.isidentifier())


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

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

print('__foo__'.isidentifier())


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

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

print('for'.isidentifier())


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

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

print('abc'.islower())


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

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

print('a@ 1,'.islower())


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

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

print('11'.isnumeric())


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

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

print('1.1'.isnumeric())


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

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

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


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

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

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


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

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

print('\t'.isspace())


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

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

print('HelloWorld'.istitle())


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

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

print('Hello World'.istitle())


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

What will be the output of the following Python code?

print('Hello!2@#World'.istitle())


  1. True
  2. False
  3. None
  4. error
Qus. 65 :

What will be the output of the following Python code?

print('1Rn@'.lower())


  1. n
  2. 1rn@
  3. rn
  4. r
Qus. 66 :

What will be the output of the following Python code?

print('''  \tfoo'''.lstrip())


  1. foo
  2. foo
  3. foo
  4. none of the mentioned
Qus. 67 :

What will be the output of the following Python code?

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


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

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. 69 :

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. 70 :

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. 71 :

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. 72 :

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. 73 :

What will be the output of the following Python code?

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


  1. (‘ab’, ‘cd’, ”)
  2. (‘ab’, ‘cd’)
  3. error
  4. none of the mentioned
Qus. 74 :

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

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


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

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. 76 :

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. 77 :

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

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


  1. abef
  2. 12
  3. error
  4. none of the mentioned
Qus. 78 :

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. 79 :

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

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


  1. xyyxyyxyxyxxy
  2. 12y12y1212x12
  3. 12yxyyxyxyxxy
  4. xyyxyyxyxyx12
Qus. 80 :

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. 81 :

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. 82 :

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. 83 :

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. 84 :

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. 85 :

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. 86 :

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. 87 :

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. 88 :

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. 89 :

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. 90 :

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

print('abcd'.translate({97: 98, 98: 99, 99: 100}))


  1. bcde
  2. abcd
  3. error
  4. none of the mentioned
Qus. 91 :

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 function that takes a string as parameter and returns a string with every successive repetitive character replaced by and

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 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 Sarkari Exam Quiz