Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between "string" and 'string' in Python? [duplicate]

In PHP, a string enclosed in "double quotes" will be parsed for variables to replace whereas a string enclosed in 'single quotes' will not. In Python, does this also apply?

like image 331
davidmytton Avatar asked Sep 27 '08 14:09

davidmytton


People also ask

How do you duplicate a string in Python?

To repeat a string in Python, We use the asterisk operator ” * ” The asterisk. is used to repeat a string n (number) of times. Which is given by the integer value ” n ” and creates a new string value.

What are the two types of strings in Python?

Strings can be created by enclosing characters inside a single quote or double-quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings.

What is the difference between and :: in Python?

Here is a list of the differences between 'and' and '&' in Python. The and is a type of Logical AND that returns in a True form whenever both the operands are also true. The &, on the other hand, is a bitwise operator used in the Python language. It basically acts on various bits and performs operations bit by bit.

What is the difference between string and string?

In this blog we shall see the difference between string and String. Well technically both of these are the same but with a slight difference. While “string” is a datatype, whereas “String” represents a class. “string” is an alias for System.


2 Answers

No:

2.4.1. String and Bytes literals

...In plain English: Both types of literals can be enclosed in matching single quotes (') or double quotes ("). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character...

like image 199
Milen A. Radev Avatar answered Sep 20 '22 17:09

Milen A. Radev


Python is one of the few (?) languages where ' and " have identical functionality. The choice for me usually depends on what is inside. If I'm going to quote a string that has single quotes within it I'll use double quotes and visa versa, to cut down on having to escape characters in the string.

Examples:

"this doesn't require escaping the single quote" 'she said "quoting is easy in python"' 

This is documented on the "String Literals" page of the python documentation:

  • http://docs.python.org/2/reference/lexical_analysis.html#string-literals (2.x)
  • http://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals (3.x)
like image 26
Bryan Oakley Avatar answered Sep 23 '22 17:09

Bryan Oakley