In Python’s interactive mode, each line is assumed to be an expression that is evaluated. The return value is provided to the user. Thus, if you evaluate a string expression or call a function or an operation that returns a string, the output will display quotes around the string to tell the user that this is a string result:
# Quotes >>> 'hello world' 'hello world'
For example, if you’d simply evaluate a mathematical operation with an integer result, no quotes would be displayed:
>>> 40 + 2 42
How can you get rid of the quotes and print a Python string without the quotes?
Solution 1: print()
When in interactive mode, a string result is displayed with single quotes around it to indicated that the result is a string. But if you pass the resulting string in a
print(...) function call as an argument, the quotes disappear.
# No Quotes >>> print('hello world') hello world
This is because the
print() function automatically converts each argument to its string representation and prints it to the standard output. Because all print outputs are strings, it doesn’t provide value to indicate the string type with enclosing types. So, Python just skips them.
Feel free to watch the following video to dive deeper into the function:
Solution 2: string.strip()
If you want to remove the enclosing quotes from a string before printing it, you can call the
string.strip() method and pass the single and double quotes characters to be stripped from the beginning and end of the string object on which it is called. For example, the expression
'"hello world"'.strip('"\'') removes all enclosing single and double quotes and results in the simple string
hi = '"hello world"' print(hi) # "hello world" print(hi.strip('"\'')) # hello world
Note that the
strip() method removes all characters in the provided string argument. As you need to enclose the string argument in double or single quotes itself, you need to escape the character you used to enclose the string argument. This removes the special meaning (=close the string) from the quote and tells Python to use the quote character.
Again, feel free to watch the following video to dive deeper into the function:
Solution 3: string.replace()
The brute-force approach to remove all quotes from a given string, not just the enclosing ones, is to use the
string.replace() method and replace every occurrence of a quote with the empty string
''. For example, to first remove all double quotes and then all single quotes from string
s, you can chain the function twice via
s.replace('"', '').replace("'", '').
>>> s = 'hello """ world \'\'\' !!!' >>> s 'hello """ world \'\'\' !!!' >>> s.replace('"', '').replace("'", '') 'hello world !!!'
Watch the following video to dive deeper into the function:
Solution 4: re.sub()
The regex function
re.sub(P, R, S) replaces all occurrences of the pattern
P with the replacement
R in string
S. It returns a new string. For example, if you call
re.sub('["\']', '', s), the result will be the new string with all single or double quotes removed from the string
>>> import re >>> s = 'hello """ world \'\'\' !!!' >>> re.sub('["\']', '', s) 'hello world !!!'
The regex pattern
'["\']' opens a character class that you can see as an OR relation—it will either match the double quote character
" or the single quote character
' (escaped to prevent a syntax error as the last single quote would open a new string that is never closed).
Yes, there’s another video for learning and improving your understanding of this vital function:
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.