Python string functions are very popular. There are two ways to represent strings in python. String is enclosed either with single quotes or double quotes. Both the ways (single or double quotes) are correct depending upon the requirement. Sometimes we have to use quotes (single or double quotes) together in the same string, in such cases, we use single and double quotes alternatively so that they can be distinguished.
Check below example and analyze the error –
#Gives Error print('It's python')
It gives an invalid syntax error. Because single quote after “it” is considered as the end of the string and rest part is not the part of a string.
It can be corrected as:
If you want to print ‘WithQuotes’ in python, this can’t be done with only single (or double) quotes alone, it requires simultaneous use of both.
'WithQuotes' Hello 'Python' "WithQuotes" Hello "Python"
The choice between both the types (single quotes and double quotes) depends on the programmer’s choice. Generally, double quotes are used for string representation and single quotes are used for regular expressions, dict keys or SQL. Hence both single quote and double quotes depict string in python but it’s sometimes our need to use one type over the other.
- Python | Printing String with double quotes
- Python | Avoiding quotes while printing strings
- Python | Ways to print list without quotes
- Triple quotes in Python
- Benefits of Double Division Operator over Single Division Operator in Python
- Python - Filter and Double keys greater than K
- Print Single and Multiple variable in Python
- Python | Convert Triple nesting to Double nesting list
- Python - Double each List element
- Python - Double each consecutive duplicate
- Python - Double Split String to Matrix
- Transpose a matrix in Single line in Python
- Multiplication of two Matrices in Single line using Numpy in Python
- Python code to move spaces to front of string in single traversal
- A single neuron neural network in Python
- Python | Convert a list of multiple integers into a single integer
- Python | Replace multiple occurrence of character by single
- Python | Split a list having single integer
- Python | Associating a single value with all list items
- Python | super() in single inheritance