What would be the output of the following code?
str= “HelloWorld”
print(“str=”,str)
print(“str[1]=”,str[1])
print(“str[-1]=”, str[-1])
Answers
Answered by
5
Output:
str = HelloWorld
str[1] = e
str[-1] = d
Explanation:
Program:
str = 'HelloWorld'
print('str = ',str)
print('str[1] = ',str[1])
print('str[-1] = ', str[-1])
Let us understand the program.
str = 'HelloWorld' #This statement is used to declare a string str and initialize it.
print('str = ',str) # This statement is used to print the initialized string.
# str[1] means second character of the string.
# This statement is used to print the second character of the string.
print('str[1] = ',str[1])
# str[-1] means last character of the string.
# This statement is used to print the last character of the string.
print('str[-1] = ', str[-1])
Refer the attached image for the output.
Attachments:
Similar questions