Avoiding quotes while printing strings in Python

PythonServer Side ProgrammingProgramming

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

 Live Demo

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

 Live Demo

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
raja
Published on 26-Feb-2020 07:37:30
Advertisements