• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Using Python to Remove Apostrophe From String

Using Python to Remove Apostrophe From String

May 5, 2022 Leave a Comment

You can easily remove an apostrophe from a string in Python with the Python replace() function.

string_with_apostrophe = "I'm looking for the dog's collar."

string_without_apostrophe = string_with_apostrophe.replace("'","")

print(string_without_apostrophe)

#Output:
Im looking for the dogs collar.

When working with strings in Python, being able to manipulate your variables easily is important. There are a number of built in string methods which allow us to get information and change string variables.

One such function which is very useful is the string replace() function. With the replace() function, we can create a new string where the specified value is replaced by another specified value.

We can use the replace() function to remove apostrophes from string variables.

To remove an apostrophe from a string, you can use replace() and pass an empty string as the replacement value as shown below.

string_with_apostrophe = "I'm looking for the dog's collar."

string_without_apostrophe = string_with_apostrophe.replace("'","")

print(string_without_apostrophe)

#Output:
Im looking for the dogs collar.

Using the replace() function to Make Replacements in Strings in Python

You can use replace() for many other cases in your Python code.

If we want to replace all the spaces with periods, we can do so easily in the following Python code.

string_with_spaces = "This is a string."

string_with_periods = string_with_spaces.replace(" ","-")

print(string_with_periods)

#Output:
This.is.a.string.

For example, if we want to replace spaces with dashes, we can do the following.

string_with_spaces = "This is a string."

string_with_dashes = string_with_spaces.replace(" ","-")

print(string_with_dashes)

#Output:
This-is-a-string.

If you’d like to remove words in a string, you can do this in a similar way as removing the apostrophes from a string.

string_with_words = "This is a string."

string_without_is = string_with_words.replace("is","")

print(string_without_is)

#Output:
This  a string.

Hopefully this article has been useful for you to learn how to apostrophes from strings in Python.

Other Articles You'll Also Like:

  • 1.  How to Filter Lists in Python
  • 2.  pandas sum – Get Sum of Series or DataFrame Columns
  • 3.  Python ljust Function – Left Justify String Variable
  • 4.  How to Check If File is Empty with Python
  • 5.  Check if Variable is Float in Python
  • 6.  Reverse a List in Python Without Reverse Function
  • 7.  Swap Two Values of Variables in Python
  • 8.  Using Python to Append Character to String Variable
  • 9.  Using Python to Remove Last Character from String
  • 10.  Using Python to Search File for String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy