Tip: Easy Repeated Strings in Python
- DeTech Theory
- Jul 7, 2022
- 1 min read
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
Comments