Similarly, if the string contains double quotes, we can use single quotes to represent the string such that we don’t have to escape the double quotes. > 'It\'s a good example.' "It's a good example." > "It's a good example." "It's a good example." The other is to use double quotes instead of single quotes as the enclosing quotes. How can we fix this error? One is to escape the single quote by placing a backslash before it. > 'It's a bad example.' File "", line 1 'It's a bad example.' ^ Synta圎rror: invalid syntax An example of failing to escape is shown below. Like other programming languages, when a string contains special characters like quotes, we need to escape them. > "mixed quotes' File "", line 1 "mixed quotes' ^ Synta圎rror: EOL while scanning string literal > 'mixed quotes" File "", line 1 'mixed quotes" ^ Synta圎rror: EOL while scanning string literal Escaping Behaviors However, it should be noted that we don’t want to mix them as it’s a syntactical error. In other words, we can use single and double quotes interchangeably when we declare a string. > quotes_single = 'a_string' > quotes_double = "a_string" > quotes_single = quotes_double TrueĪs you notice, the strings created by using single and double quotes are the same. As shown in the code below, we create these two strings using single and double quotes, respectively. The most common use of single and double quotes is to represent strings by enclosing a series of characters.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |