[Tutor] how do I input " as part of a string?
Alan Gauld
alan.gauld at btinternet.com
Sat Jul 14 00:00:00 CEST 2007
"elis aeris" <hunter92383 at gmail.com> wrote
> how do I input " as part of a string?
>
> f.write("my "car" has been a great one for me")
>
> I am trying to write the above lines into a file, including
> the " as part of a the single string
As you say in the subject the qurstion is how to get a
quote into a string. The fact you are writing to a file is
irrelevant so lets get that out of the way and look at
how you print a string with a quote in it.
I actually mention one approach in the Simple Sequences
topic of my tutorial - the very first hands on topic. You might
find it helpful to read that through.
But basically you can include one kind of quote inside
another, like this:
>>> s = 'This has a " inside'
>>> s2 = "But this has a ' in it"
>>> s3 = '''And this has both " and ' characters'''
>>> s4 = """and so does this: ' ", see?"""
And I can print all of them:
>>> for x in s,s2,s3,s4: print x
Another way to include a quote is to "escape" it, that is
precede it with a \ character, like this:
>>> s5 = "This has \" in it despite having \" as its quote sign"
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list