Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix syntax error when printing a string with an apostrophe in it? [closed]

When I am trying to print a string like the one below which uses an apostrophe in the sentence,

print(''I am jack's raging bile duct'')

I get a syntax error. How to fix this?

like image 348
user1940012 Avatar asked Dec 31 '12 18:12

user1940012


People also ask

How do you print an apostrophe in a string in python?

By using the escape character \" we are able to use double quotes to enclose a string that includes text quoted between double quotes. Similarly, we can use the escape character \' to add an apostrophe in a string that is enclosed in single quotes: print('Sammy\'s balloon is red. ')

Why does my print statement have a syntax error?

The Python “SyntaxError: Missing parentheses in call to 'print'” error is raised when you try to print a value to the console without enclosing that value in parenthesis. To solve this error, add parentheses around any statements you want to print to the console. This is because, in Python 3, print is not a statement.

How do I get rid of the apostrophe in Python?

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.


1 Answers

You can use both " and ' to write a string in Python, a double ' ('') will be invalid.

If you use ", the syntax for your case would be

print("I am jack's raging bile duct")

But if you use ', you may need to escape the apostrophe as follows:

print('I am jack\'s raging bile duct')

In general, if you use ", and your string has also ", you will need to escape every " in your string, except the one that closes, same happens with '.

like image 59
iferminm Avatar answered Sep 23 '22 20:09

iferminm