Q.18- What will be the output of the following Python code?
string = "my name is x"
for i in string:
print (i, end=", ") *
a) m, y, , n, a, m, e, , i, s, , x,
b) m, y, , n, a, m, e, , i, s, , x
c) my, name, is, x,
d) error
Answers
The output of the following Python cŏde is m, y, , n, a, m, e, , i, s, , x,
a) m, y, , n, a, m, e, , i, s, , x,
Let's correct the question
5. What will be the output of the following Python cŏde?
string = "my name is x"
for i in string:
print (i, end=", ")
a) m, y, , n, a, m, e, , i, s, , x,
b) m, y, , n, a, m, e, , i, s, , x
c) my, name, is, x,
d) error
Explanation
for is a loop which is looping characters from the string
end is a parameter that can be defined but, if not defined then it has a default value which is a newline character i.e. ‘\n’
As we are looping the string with variable i, it takes the value of one character at a time.
It is working like this
Loop starts takes the first character 'm' from string and then ends it with a ',' and then takes 'y' and then ends with a ',' and then takes empty character and ends with a ',' and so on.
Python is created by Guido van Rossum and came into existence in 1991. It is a high-level and general programming language. It is a very lucid programming language as it has a good language construct and object-oriented approach. It is dynamically typed and garbage-collected.
a) m, y, , n, a, m, e, , i, s, , x,
Explanation:
- String variables can hold sequences of text A string literal is an actual strings in your program. The input() function reads strings from a user .
- The Len() function gets the length of a string. Strings are immutable String concatenation glues strings together with the + operator.
Strings :
- A string is a group/a sequence of characters. Since Python has no provision for arrays, we simply use strings. This is how we declare a string. We can use a pair of single or double quotes. Every string object is of the type 'str'.
- Variable i takes the value of one character at a time.
string = "my name is x"
for i in string:
print (i, end=", ")
m, y, , n, a, m, e, , i, s, , x,