Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to print a string two times with single statement in Python?
When developing Python applications, you may need to repeat or duplicate a string for display or formatting purposes. Python provides several ways to print a string twice with a single statement.
There are different approaches to print a string twice with a single statement in Python:
- Using Multiplication Operator
- Using the Concatenation Operator
- Using the format() Method
- Using f-string
- Using join() Method with a List
- Using List Unpacking
- Using Tuple Unpacking
- Using the map() Function
Using the Multiplication Operator
In Python, you can perform string multiplication using the * operator. This is the simplest and most direct way to repeat a string a specified number of times ?
print("Tutorialspoint " * 2)
Tutorialspoint Tutorialspoint
Using the Concatenation Operator
You can manually concatenate the string with itself using the + operator ?
print("Tutorialspoint " + "Tutorialspoint ")
Tutorialspoint Tutorialspoint
Using the format() Method
The format() function can insert values into placeholders using curly braces ?
print("{} {}".format("Tutorialspoint", "Tutorialspoint"))
Tutorialspoint Tutorialspoint
Using f-strings
F-strings provide a way to embed expressions inside string literals. These can be used in Python versions 3.6+ ?
message = "Tutorialspoint"
print(f'{message} {message}')
Tutorialspoint Tutorialspoint
Using the join() Method with a List
The join() method joins elements of a list into a single string ?
print(" ".join(["Tutorialspoint"] * 2))
Tutorialspoint Tutorialspoint
Using List Unpacking
You can use the unpacking operator * with a list to replicate the string ?
print(*["Tutorialspoint"] * 2)
Tutorialspoint Tutorialspoint
Using Tuple Unpacking
Tuple unpacking is similar to list unpacking, but uses a tuple instead of a list ?
print(*("Tutorialspoint",) * 2)
Tutorialspoint Tutorialspoint
Using the map() Function
The map() function applies the str() function to each list element and unpacks the result ?
print(*map(str, ["Tutorialspoint"] * 2))
Tutorialspoint Tutorialspoint
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| Multiplication (*) | High | Fast | Simple repetition |
| Concatenation (+) | High | Moderate | Few repetitions |
| f-string | High | Fast | Complex formatting |
| join() | Medium | Fast | Many repetitions |
Conclusion
The multiplication operator (*) is the most efficient and readable method for repeating strings. For complex formatting needs, use f-strings. The join() method works well for multiple repetitions with custom separators.
