Strings Output#

Section Title: Strings Output

  • Find the output of the following code.

  • Please don’t run the code before giving your answer.     

Question-1#

print('Mark'+'Twain')

Solution

MarkTwain

Question-2#

print('Mark '+'Twain')

Solution

Mark Twain

Question-3#

print('Mark\tTwain')

Solution

Mark  Twain

Question-4#

print('Mark\nTwain')

Solution

Mark
Twain

Question-5#

print('Mark\bTwain')

Solution

MarTwain

Question-6#

print('Mark\b\bTwain')

Solution

MaTwain

Question-7#

print('Mark\b\b\bTwain')

Solution

MTwain

Question-8#

print('Mary\'s book.')

Solution

Mary’s book.

Question-9#

print(r'Mary\'s \n book.')

Solution

Mary’s \n book.

Question-10#

text = 'Hello World.'
len(text)

Solution

12

Question-11#

x = 'abcdefg'

print(x[:])
print(x[:-3])
print(x[-4:])
print(x[2:4])
print(x[-4:-2])

Solution

abcdefg
abcd
defg
cd
de

Question-12#

name = 'Mary'
print(f'My name is (name)')

Solution

My name is (name)

Question-13#

name = 'George'
salary = 2000

print(f'My name is {name}. I earn ${salary} per month.')

Solution

My name is George. I earn $2000 per month.

Question-14#

x = 'california'

print(x.find('f'))
print(x.find('A'))
print(x.find('a', 4))

Solution

4
-1
9

Question-15#

state = 'utah'
print(3*'utah')

Solution

utahutahutah

Question-16#

name = 'MaRk tWAin'

print(name.upper())
print(name.lower())
print(name.capitalize())
print(name.title())

Solution

MARK TWAIN
mark twain
Mark twain
Mark Twain

Question-17#

country = 'Morocco'

print(country.find('r'))
print(country.find('o'))
print(country.find('c'))
print(country.find('o',4))
print(country.find('t'))

Solution

2
1
4
6
-1

Question-18#

name = '   Liz   '
print(name.strip())

Solution

Liz

Question-19#

name = '   Liz   '
print(name.lstrip()+'T')

Solution

Liz  T

Question-20#

name = 'ryan'
print(name.startswith('R'))

Solution

False

Question-21#

word = 'ab,cde=abc,de'

index1 = word.find('f')
index2 = word.find('=')
index3 = word.find(',',index2)

print(word[index2:index3+index1])

Solution

=ab

Question-22#

state = 'Florida'

x = state[1:4:2].upper().lower()
y = state[-1:-5:-1].capitalize().swapcase()

print(y+'_'+x)
print(x+y)

Solution

aDIR_lr
lraDIR