0

In Python,sometimes I see the string variable using ' ' to define it; sometimes I see the string variable using " " to define it.

For instance, it seems to me that both of the followings work

file_to_read = os.path.join(path, 'file.txt')

file_to_read = os.path.join(path, "file.txt")

My question is that are ' 'and " " totally interchangeble?

wim
  • 338,267
  • 99
  • 616
  • 750
user297850
  • 7,705
  • 17
  • 54
  • 76
  • Yes. In terms of creating strings they are. However you can nest quotes of different types, but not quotes of the same types. – Christian Dean Feb 07 '17 at 03:30
  • Yes and no. Mostly yes, but inside double quotes you have to escape double quotes, inside single quotes you have to escape single quotes. Then there are "triple" quotes than can act as multi-line strings. Try reading the [docs](https://docs.python.org/3/tutorial/introduction.html#strings) – juanpa.arrivillaga Feb 07 '17 at 03:35

1 Answers1

0

Yes, in python single quote(') and double quotes (") are totally interchangeable. (Bear in mind, for either, you have to escape the quotes that you surround the string by)

However a common practice is to surround the string that will be displayed to user in double quotes and other "internal" strings in single quotes

Bishal
  • 807
  • 1
  • 5
  • 20
  • @JohnColeman They are not the same thing because you can not have nested quotes of the same type without escaping them with `\\` first. – the_constant Feb 07 '17 at 03:38