How to Put an Apostrophe in a String in Python

In Python, incorporating an apostrophe into a string can sometimes pose a problem as single and double quotes are often used to define strings.

However, there are some methods you can apply to successfully incorporate an apostrophe into your Python string without creating a syntax error. This tutorial will walk you step-by-step through these methods.

Method 1: Using Backslash (\)

In Python language, the backslash (\) is often used as an escape character. When we need to incorporate an apostrophe into a string, we can precede it with a backslash. The Python interpreter will thus understand that the following character is part of the string and not the end of the string.

Method 2: Using double quotes (“”)

Another way to add an apostrophe into a Python string is by defining the string using double quotes. In this case, any apostrophe included in the string will not be considered the string’s end.

Method 3: Using triple quotes (”’ ”’ or “”” “””)

Triple quotes are often used to define multiline strings. However, they can also be used to make a string that includes both single and double quotes.

Complete Examples

Output Results

That's my book
That's my book
That's "my" book

Conclusion

In conclusion, while working with strings in Python, it’s crucial to remember that both single and double quotes can be used to denote a string.

If you need to incorporate an apostrophe into your Python string, consider applying one of the three methods: using a backslash, employing double quotes, or using triple quotes.