String is the most commonly used data type in Python. We can use quotation marks ( ' or " ) to create strings.
Creating a string is simple, just assign a value to a variable. For example:
var1 = 'Hello World!' var2 = "Runoob"
Python accesses a string Value
Python does not support single character types, and a single character is also used as a string in Python.
Python accesses substrings, and you can use square brackets to intercept the string, as shown in the following example:
var1 = 'Hello World!' var2 = "Runoob" print ("var1[0]: ", var1[0]) print ("var2[1:5]: ", var2[1:5])
Execution results of the above example
var1[0]: H var2[1:5]: unoo
Python string update
You can intercept part of the string and splice it with other fields, as shown in the following example:
var1 = 'Hello World!' print ("已更新字符串 : ", var1[:6] + 'Runoob!')
The execution result of the above example
已更新字符串 : Hello Runoob!
python escape characters
When special characters need to be used in characters, python uses backslash (\) to escape Escape characters. The following table:
Escape character | Description |
---|---|
\ (At the end of the line) | Line continuation character |
\\ | Backslash symbol |
\' | Single quotes |
\" | Double quotes |
\a | Ring |
\b | Backspace |
\000 | empty |
\n | Line break |
\v | Vertical tab character |
\t | Horizontal tab |
\r | Enter |
\f | Page feed |
\oyy | Octal number, the character represented by yy, for example: \o12 represents line feed |
\xyy | Hexadecimal number, the character represented by yy, for example: \x0a represents a newline |
\other | Other characters are output in normal format |
The above is the detailed content of How to enter a string in python. For more information, please follow other related articles on the PHP Chinese website!