Open In App

Avoiding Quotes while Printing Strings

Last Updated : 01 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

When printing strings in Python, quotes are usually omitted in standard output. However, when printing lists or dictionaries, string values appear enclosed in quotes. For example, consider the list [“Paris”, “London”, “New York”]. If printed directly, it appears as [‘Paris’, ‘London’, ‘New York’] with quotes. Let’s explore different ways to print strings without quotes efficiently.

Using print() with join()

join() method allows us to concatenate list elements into a single string, effectively removing quotes.

Python
a = ["Paris", "London", "New York"]
print(" ".join(a))

Output
Paris London New York

Explanation:

  • join() merges elements of a into a single string.
  • The separator ” ” ensures elements are separated by spaces.

Let’s explore some more ways and see how we can avoid quotes while printing strings in Python.

Using unpacking operator *

The unpacking operator * allows printing list elements without additional characters like brackets or quotes.

Python
a = ["Paris", "London", "New York"]
print(*a)

Output
Paris London New York

Explanation:

  • The * operator unpacks list elements.
  • print() displays them as separate arguments, avoiding quotes.

Using map() with print()

map() function can convert list elements to string format and print them directly.

Python
a = ["Paris", "London", "New York"]
print(*map(str, a))

Output
Paris London New York

Explanation:

  • map(str, a) ensures all elements are treated as strings.
  • * unpacks and prints them without quotes.

Using re.sub() from re module

For more complex structures like dictionaries, re.sub() can remove quotes selectively.

Python
import re

a = {"name": "Nikki", "city": "Paris"}
print(re.sub(r"[']", "", str(a)))

Output
{name: Nikki, city: Paris}

Explanation:

  • str(a) converts the dictionary to a string.
  • re.sub(r”[‘]”, “”, …) removes single quotes.


Next Article
Practice Tags :

Similar Reads