Python strings

Defining strings in Python

Earlier we explained what a string is. Strings can be represented by ''or ""enclosed.

What if the string itself contains 'it? For example, we want to represent a string, I'm OK  in which case it can be " "enclosed:

"I'm OK"

Similarly, if the string is included  ", we can use ' 'it to indicate it:

'Learn "Python" in imooc'

What if the string contains both 'and contains "?

At this time, you need to ” escape ” some special characters of the string , and the Python string is used \for escaping.

To indicate that a string is  Bob said "I'm OK".
ambiguous due to ‘ and ‘, we insert a \string in front of it to indicate that this is a normal character and does not represent the beginning of the string, so the string can be represented as

'Bob said \"I\'m OK\".'

Note: The escape character \ is not counted in the contents of the string.

Commonly used escape characters are:

\n means line break
\t indicates a tab
\\ means \ character itself

Leave a Reply

Your email address will not be published. Required fields are marked *