Why does the type of quote change the value of the string? - Python

python add single quotes to string
python print string with double quotes
python escape quotes in string
python add quotes to string in list
python triple quote format
python print string without quotes
python raw string
python backslash in string
In[1] : '\"""'
Out[1]: '"""'

In[2] : "\""""
Out[2]: '"'

In the following examples (in which I'm using python 3), why does changing the type of quote used to enclose a string change the actual value of the string? I'd expect "\""""=='\"""' to be true, but it's false. And why does the second example return only a single double quote? Thanks!

The expression "\"""" is two string literals, "\"" and "" (or '"' and '', switching to single quotes for clarity), concatenated together. The double-quoted equivalent of '\"""' would be "\"\"\"".

>>> ("\""
... ""
... )
'"'
>>> "\"\"\""
'"""'

How To Format Text in Python 3, How do you print a double quoted string in Python? In Python, double quote and single quote are the same. There's no different between them. And there's no point to replace a single quote with a double quote and vice versa: 2.4.1. String and Bytes literalsIn plain English: Both types of literals can be enclosed in matching single quotes (') or double quotes (").

Python does something the same way C does, in terms of string concatenation:

'hello' 'world'  ==  'helloworld'

That is, if you put two quoted string literals next to each other, with nothing in between, they will be concatenated together. This is the difference between 1 and 2:

In[2] : "\""""  --> "\"" "" 

So your second input is actually concatenating the string " with an empty string.

Strings in Python, . However, if you use the print() function only contents of the string is displayed. Most python coders code it that way. There is no explicit rule, but because we often read the code that way, it becomes an habit. – e-satis Mar 8 '10 at 14:35. I wonder if the single quotes for symbol-like things actually comes from the quote expression shortcut in Lisp/Scheme. In any case, it's intuitive. Also, me mateys, if we're following

In[2] is actually two adjacent strings, similar to "foo""bar".

"\"""" contains a first string "\"" and a second string "".

When evaluating that, Python concatenates them:

In[1]: "foo""bar"
Out[1]: 'foobar'

In your case, since the 2nd string is empty, you just get the first.

Triple Quotes For Multi-Line String in Python, How do you remove quotes from a string in Python? In Python, a string type object is a sequence (left-to- right order) of characters. Strings start and end with single or double quotes Python strings are immutable. Single and double quoted strings are same and you can use a single quote within a string when it is surrounded by double quote and vice versa.

The single quote can end only with an other single quote,

hence '\" is not a string and python then waits for the other ' to finish the string

so '\"""' is one string and "\"""" is two strings, "\"" and ""

the results are then """ because python has to take \""" in the string object, whereas the other is just a " with an empty string concatenated

I hope my answer is clear, it is not easy to explain with all these quotation marks

How to remove quotes from a string in Python, So should I use single quotes or double quotes while creating strings? As Python is a strongly typed language, it's can't just convert data of one type to Let's see what will happen if we try to modify an existing string object s by adding​  If you need both kinds of quotes in your string, use a triple-quoted string: If you want to include both kinds of triple-quoted strings in your string (an extremely unlikely case), you can't do it, and you'll have to use non-raw strings with escapes.

2.4.1 String literals, while the string value is Hello, World! without the quotation marks. Because we can use single quotes or double quotes within Python, it is simple to the string data type is a very important building block of programming. Strings are sequences of character data. The string type in Python is called str. String literals may be delimited using either single or double quotes. All the characters between the opening delimiter and matching closing delimiter are part of the string: >>> >>>

A Functional Start to Computing with Python, In plain English: String literals can be enclosed in matching single quotes and a double quote; r"\" is not a value string literal (even a raw string cannot end in  Python has a built-in string class named "str" with many handy features (there is an older module named "string" which you should not use). String literals can be enclosed by either double or single quotes, although single quotes are more commonly used. Backslash escapes work the usual way within both single and double quoted literals -- e.g

2. Variables, expressions and statements, Python got confused by starting a string with a single quote, then trying to The dictionary type is a simplified form of a database: it associates some data with a we see how keys and values in a dictionary can change as a program runs. A comment may appear at the start of a line or following whitespace or code, but not within a string literal. A hash character within a string literal is just a hash character. Since comments are to clarify code and are not interpreted by Python, they may be omitted when typing in examples.