Home » Python » Remove Single Quotes from Single in Python

Remove Single Quotes from Single in Python

To remove single quotes from a string in Python, use str.replace(oldValue,newValue) to find occurrences of single quotes (“‘”) and replace them with “”.

str.replace() in Python replaces the specified phrase in the string with the new phrase. Use the replace() method for removes single quotes from a string in Python. For example, removing all single quotes from a string “‘” with ”

In this example, we will discuss how to use the str.replace() in Python to remove single quotes from a string.

Use str.replace() to remove single quotes from a string in Python

To remove single quotes from a string in Python using replace():

  1. Call the str.replace() on a string and pass “‘” search for and a new character ‘ ‘ for a replacement.
  2. The replace() returns a copy of the string in which all occurrences of single quotes are replaced by a new character ”.
str = "Hi! Welcome to 'Python' Programming"

str1 = str.replace("'",'')
print(str1)

In the above Python program, we pass the following parameters to replace():

  1. str variable stores the string that contains single quotes.
  2. Use str.replace() to remove single quotes.
  3. the character “‘” to search for in the string.
  4. replacement character ” . It will replace all occurrences of single quotes “‘” with ‘ ‘

It will return the copy of the string and store it in the str1 variable and print using print() Python.

The output of the above program after removing single quotes in a string in Python is:

Hi! Welcome to Python Programming

Cool Tip: How to remove double quotes from a string in Python!

Conclusion

I hope the above article on how to remove single quotes in a string in Python using str.replace() is helpful to you.

You can find more topics about the Python tutorials on the DataVisualizr Home page.

Leave a Comment