Print a string without quotes in Python

avatar

Borislav Hadzhiev

Last updated: Sep 3, 2022

banner

Photo from Unsplash

Print a string without quotes in Python #

To print a string without quotes:

  1. Use the str.replace() method to remove the quotes from the string.
  2. Use the print() function to print the result.
main.py
my_str = '"one "two" three"' # 👇️ remove all double quotes from string result = my_str.replace('"', '') print(result) # 👉️ one two three # -------------------------------------------- # 👇️ remove leading and trailing double and single quotes from string result = my_str.strip('"\'') print(result) # 👉️ one "two" three # -------------------------------------------- # 👇️ remove quotes from list of strings my_list = ['"one"', '"two"', '"three"'] new_list = [item.replace('"', '') for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three']

The first example uses the str.replace() method to remove all double quotes from the string before printing it.

main.py
my_str = '"one "two" three"' result = my_str.replace('"', '') print(result) # 👉️ one two three

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

You can use the same approach if you need to remove all single quotes from the string.

main.py
result = "'one 'two' three'".replace("'", '') print(result) # 👉️ one two three

If you need to remove all double and single quotes from the string, use 2 calls to the str.replace() method.

main.py
result = """'one "two" three'""".replace("'", '').replace('"', '') print(result) # 👉️ one two three

Alternatively, you can use the str.strip() method.

Print a string without quotes using str.strip() #

To print a string without quotes:

  1. Use the str.strip() method to remove the leading and trailing quotes from the string.
  2. Use the print() function to print the result.
main.py
my_str = '"one "two" three"' result = my_str.strip('"\'') print(result) # 👉️ one "two" three

The str.strip method returns a copy of the string with the specified leading and trailing characters removed.

We used the str.strip() method to remove the leading and trailing single and double quotes from the string before printing it.

If you need to remove all quotes from the string, use the str.replace() method instead.

Use a list comprehension if you need to remove all quotes from a list of strings before printing the result.

main.py
my_list = ['"one"', '"two"', '"three"'] new_list = [item.replace('"', '') for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three']

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the replace() method to remove all quotes from the current string.

The list comprehension returns a new list, in which the strings don't contain quotes.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.