Python Practice › String Programs › Program 3
Four copies of the last two characters
Write a Python program to make a string of 4 copies of the last two characters of a string (length must be at least 2).
Input
"Python"
Output
onononon
Approach
- Slice the last two characters and repeat them with
* 4.
Solution
text = "Python"if len(text) >= 2: print(text[-2:] * 4)else: print("String must have at least 2 characters")Output
onononon
Line by line
Line 3
text[-2:] is “on”; multiplying a string repeats it.
Learn the concept
New to this topic? Read the lesson, then come back.