Computer Science
Consider the string mySubject:
mySubject = "Computer Science"
What will be the output of:
print(mySubject[:3])print(mySubject[-5:-1])print(mySubject[::-1])print(mySubject*2)
Python String Manipulation
2 Likes
Answer
ComiencecneicS retupmoCComputer ScienceComputer Science
Explanation
- The slice
mySubject[:3]takes characters from the start ('C') of the string up to index 2 ('m') and prints it. - The code
print(mySubject[-5:-1])uses negative indexing to slice the string. It starts from the fifth character from the end ('i') and ends before the last character ('c'), outputtingienc. - The code
print(mySubject[::-1])reverses the string using slicing. The[::-1]notation means to start from the end and move backwards, outputtingecneicS retupmoC. - When an integer is multiplied by a string in Python, it repeats the string the specified number of times. Hence, the code
print(mySubject*2)repeats the string stored in mySubject twice.
Answered By
1 Like
Related Questions
Explain the membership operators in String.
Differentiate between append() and extend() methods and provide examples of each.
Consider the dictionary
stateCapital:stateCapital = {"AndhraPradesh" : "Hyderabad", "Bihar" : "Patna", "Maharashtra" : "Mumbai", "Rajasthan" : "Jaipur"}Find the output of the following statements:
print(stateCapital.get("Bihar"))print(stateCapital.keys())print(stateCapital.values())print(stateCapital.items())
What are the ways by which websites track us?