A String is a sequence of characters. In Python, single and double quotes can be used to begin and end a string literal. In Python programming, there are two ways to represent a string.
Strings are enclosed by single or double quotes. Depending on the situation, either method (single or double quotations) is correct. When we need to employ quotes (single or double quotes) in the same string, we use single and double quotes alternately so that they can be differentiated.
Single Quotes in Python:
Single quotes are used to indicate a quote within a quote or a direct quote in the headline of a news story.
When writing Python code, we usually use single quotes for string literals.
Note: When you know your string may contain double quotes with in, always use single quotes.
Examples:
print('It's an amazing Experiance')
Output:
File "/home/bf98c69d152913534d5c8fc67c3f5003.py", line 1 print('It's an amazing Experiance') ^ SyntaxError: invalid syntax
Explanation:
It raises an error since the single quote following "It" is treated as the end of the string and the rest of the section is not a string. Hence in this case, we should use double quotes(" ").
print('welcome to Python-Programs') print('helo123#@%')
Output:
welcome to Python-Programs helo123#@%
Double Quotes in Python:
A double quote is to set off a direct (word-for-word) quotation.
Note: When you know there will be single quotations within your string, use double quotes to enclose it.
Examples:
# Give the string as static input and store it in a variable. gvn_str1 = "good morning all" # Print the given string print(gvn_str1)
Output:
good morning all
# Give the string as static input and store it in a variable. gvn_str = "hello this is "btechgeeks", good morning all" # Print the given string print(gvn_str)
Output:
File "/home/jdoodle.py", line 2 gvn_str = "hello this is "btechgeeks", good morning all" ^ SyntaxError: invalid syntax
Explanation:
It raises an error since the double-quote before "btechgeeks" is treated as the end of the string
If you want to print ‘WithQuotes’ in Python, you can’t accomplish it with just single (or double) quotes; you must use both at the same time.
gvn_str = "hello this is 'btechgeeks'" print(gvn_str) gvn_str = 'I like "Dhoni" in Indian cricket Team' print(gvn_str)
Output:
hello this is 'btechgeeks' I like "Dhoni" in Indian cricket Team
Single vs Double Quotes in Python:
                   Single Quotes |             Double Quotes |
Single quotes are Represented with ‘ ’ |  Double quotes are Represented with ‘ ’ |
For anything that behaves like an Identifier, use single quotes. | In general, we use double quotations for text. |
For regular expressions, dict keys, and SQL, single quotes are utilized. | The string representation is done with double quotes. |
Example:Â ‘I like “Dhoni” in Indian cricket Team’ | Example:Â “hello this is ‘btechgeeks'” |
In Python, the difference between single and double quotes is not big. It is entirely situational in which we employ single and double quotations.
Python Triple Quotes:
What if you need to utilize strings with both single and double quotes? Python supports the usage of triple quotes for this purpose. A simple example of this is provided below. In addition, instead of being limited to single lines, triple quotes allow you to add multi-line strings to Python variables.
Examples
gvn_str1 = '''Excuse me, "Did you see my mobile?"''' print(gvn_str1) gvn_str2 = '''"She look's beautiful", he said.''' print(gvn_str2)
Output:
Excuse me, "Did you see my mobile?" "She look's beautiful", he said.