top of page
Search

Tip: Easy Repeated Strings in Python

If you've ever wondered if there was an easy way to do repeated strings in Python, you've come to the right place.


For a string variable


message = "HelloWorld" 

You may have been trying to do it something like this to repeat it:


for i in range(5):
    message = message + message 

Printing the variable "message" gives the output:


HelloWorldHelloWorldHelloWorldHelloWorldHelloWorld

However, in Python, this can be simplified to just one line as follows:


    message = message * 5

Printing message would then lead to the same output!



That's it folks!


Best, Vasundhara






 
 
 

Recent Posts

See All
Java Switch Case

Ever needed to write a million if-else statements to the point where your brain is breaking and you're about an inch away from throwing...

 
 
 

Comments


bottom of page