0

I have a Python program, which reads text from a file copied from the Internet and stores it in a variable for further use. It is very simple and goes like this:

Line=(Storefile.readline())
Word=Line[:-1]

([:-1] to get rid of the carriage return symbol ('\n'))

If I write Word in IDLE it returns - 'bread'

All works well, untill the read line has an apostrophe in it. In which case writing Word in IDLE gives - "bread's". 'bread' versus "bread's" - single vs. double quotes

If I compose the Word variable on a char by char basis:

for n in range(len(Word)):
Word=Word+Word[n]

The moment Word[n] is the apostrophe - single quotes turn to double quotes. 'bread' vs "bread'".

And the question is - how can I convert double quotes to a single quote? Word.replace() doesn't work, because the quotes are not part of the string - I'm at a loss...

Gloom_Demon
  • 1
  • 1
  • 1
  • 3
    This is just a display issue, and has nothing to do with the string itself. – L3viathan Oct 26 '16 at 17:53
  • 1
    Single quote vs double quote strings are just strings. – ospahiu Oct 26 '16 at 17:53
  • @L3viathan is correct. IDLE supplies the quotation marks only to show the humans that this is a string value. It uses single quotations unless there's a single-quotation mark in the string. In that case, it switches to doubles for clarity. Either single or double is a valid representation of a string value. – Prune Oct 26 '16 at 17:55
  • One fun experiment to try: give it in put with both single and double quotation marks, such as **He said, "I like the bread's consistency" -- but I didn't believe him.** – Prune Oct 26 '16 at 17:55
  • Possible duplicate of [Single quotes vs. double quotes in Python](http://stackoverflow.com/questions/56011/single-quotes-vs-double-quotes-in-python) – ospahiu Oct 26 '16 at 17:56

1 Answers1

2

From the documentation, Python strings can be in either single quotes or double quotes and it makes no difference. A string is just a sequence of characters stored as an object and when you try display it, your IDLE is making the choice of displaying them surrounded by single quotes. The string, internally and on a philosophical level, is the same.

Usually convention is to use single quotes if the string itself contains double quotes (or vice versa) so you don't have to backslash escape them. For ex. `

s1 = 'His "name" was Harambe' 
s2 = "He's no longer with us"

is easier to write than

s1 = "His \"name\" was Harambe" 
s2 ='He\'s no longer with us'
gowrath
  • 3,136
  • 2
  • 17
  • 32
  • Thank You all for the answers! I was comparing a separate list variable (List[n]) with a list of lists variable Matrix[n][1]. The separate List variable added square brackets, while the list of lists did not - so when I compared these variables with the IF statement the result was False - even though the variables had the same string in them. So I thought it was because the quotes - but now I see that it is because of the brackets. I guess the question can be deleted - Stackoverflow.com is amazing! – Gloom_Demon Oct 26 '16 at 18:40
  • @Gloom_Demon No need to delete the question; it might help someone in the future :) – gowrath Oct 26 '16 at 18:50