Four copies of the last two characters

EasyString Programs · 3 of 106

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).

Advertisement
Input
"Python"
Output
onononon

Approach

  1. Slice the last two characters and repeat them with * 4.

Solution

Python
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.

More string programs

← All 106 String Programs

Leave a Comment