Write output for the following python code.
str = “Python is easy to learn”
print(str[3:], “and ”, x[:2])
print(str[-8:] ,“and”, str[-4:-1])
Answers
Answered by
3
The output of the given code:
Output:
hon is easy to learn and Py
to learn and ear
Explanation:
In the given code, there is some error, so the correct code can be described as follows:
code:
str = 'Python is easy to learn' #defining str variable that holds a string value
print(str[3:], 'and', str[:2]) #defining print method using slicing
print(str[-8:] ,'and', str[-4:-1]) #defining print method using slicing
Explain:
- In the above python code, the str variable is declared, that stores a string value, in the next section a print method is declared, that uses str to provide the slicing.
- In the first print method, it will start from str index value 3 and go to the last character in this 'and' is used as a message, then it prints str first two characters.
- In the second method, the str index value -8, which means, it will value index 8 to last, and in this 'and' is used as a message. then it will print the last 3 characters.
Lean more:
- Program: https://brainly.com/question/12951182
Similar questions