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
Selected Reading
Avoiding quotes while printing strings in Python
If we print a given list of strings as it is, we have to use quotes and fill in a pair of matching quotes appropriately. We can avoid using quotes in the print statements by following two approaches.
Using join()
The join method helps us in printing the output of list elements by using any separator we choose. In the below example we choose ** as separator.
Example
list = ['Mon', 'Tue', 'Wed']
# The given list
print("The given list is : " + str(list))
print("The formatted output is : ")
print(' ** '.join(list))
Output
Running the above code gives us the following result −
The given list is : ['Mon', 'Tue', 'Wed'] The formatted output is : Mon ** Tue ** Wed
Using sep Keyword
The sep keyword can also be used to give a formatted output instead of the heavy use of the quotes.
Example
list = ['MOn', 'Tue', 'Wed']
# The given list
print("The given list is : " + str(list))
print("The formatted output is : ")
print("list, sep =' - '")
Output
Running the above code gives us the following result −
The given list is : ['MOn', 'Tue', 'Wed'] The formatted output is : MOn ** Tue ** Wed MOn - Tue - Wed
Advertisements
