Computer Science
What will be the output produced by following code fragments?
y = str(123)
x = "hello" * 3
print (x, y)
x = "hello" + "world"
y = len(x)
print (y, x)
Python
Python String Manipulation
37 Likes
Answer
hellohellohello 123
10 helloworld
Working
str(123) converts the number 123 to string and stores in y so y becomes "123". "hello" * 3 repeats "hello" 3 times and stores it in x so x becomes "hellohellohello".
"hello" + "world" concatenates both the strings so x becomes "helloworld". As "helloworld" contains 10 characters so len(x) returns 10.
Answered By
13 Likes
Related Questions
What is the result of the following expression?
s = '0123456789' print(s[3], ", ", s[0 : 3], " - ", s[2 : 5]) print(s[:3], " - ", s[3:], ", ", s[3:100]) print(s[20:], s[2:1], s[1:1])
What is the result of the following expression?
s ='987654321' print (s[-1], s[-3]) print (s[-3:], s[:-3]) print (s[-100:-3], s[-100:3])
What will be the output produced by following code fragments?
x = "hello" + \ "to Python" + \ "world" for char in x : y = char print (y, ' : ', end = ' ')
What will be the output produced by following code fragments?
x = "hello world" print (x[:2], x[:-2], x[-2:]) print (x[6], x[2:4]) print (x[2:-3], x[-4:-2])