When most people learn the C language, the first data type they come into contact with is the string, because most tutorials use the "Hello world" program as the entry program, and the "Hello world" to be printed in this program is the string . If you have done research on natural language processing and have done related experiments with Python, you will definitely appreciate the obvious advantages of Python over other languages in string processing. Today we will take a look at strings in Python and see its usage.
1. How to declare strings in Python
There are usually three ways to declare a string in Python: put the single quote, double quote or triple quote around it.
Such as:
It can be seen from the above that the effects of the three declaration methods are exactly the same. In Python, the three declaration methods are used to declare the strings to have the same meaning, namely "hello world" and "hello world" and "'hello world" 'There is no difference. But some people will ask: Since they are completely equivalent, why are there three ways to declare? Let's take a look at these few examples:
These methods are provided in Python to make it more convenient and flexible to use (of course, you can also use escape characters to solve the above error).
The thing to note here is that
There is no string like char in C language in Python, which means that even a single character is a string.
Once a string in Python is declared, it cannot be changed, that is, the content cannot be changed by reassigning a certain position.
2. String type in Python
Strings in Python have two data types: str type and unicode type. The ASCII encoding used by the str type means that it cannot represent Chinese. The unicode type uses unicode encoding and can represent any character, including Chinese, Japanese, Korean, etc.
The default ASCII encoding used by strings in python, if you want to display the declaration as unicode, you need to add'u' or'U' in front of the string.
Look at a piece of code below
print 'I'
print u'I'
print 'python'
print u'python'
The result of this code is:
It can be seen from the running results that if it is Chinese, the unicode type is not used, and the output will be garbled.
3. Escape characters and original strings
As in the C language, Python also has escape characters. Use a backslash'' to indicate the escape of the following characters.
For example, the problem in the above example can be solved with escaping:
If you have written Java programs and used regular expressions in Java, you may hate Java regular expressions (myself), because a little carelessness will make mistakes, there are too many escapes, and see It is also very cumbersome and messy. In Python, you don’t have to worry about this problem anymore, because Python provides primitive strings. As the name suggests, it retains the meaning of primitive characters. It does not escape backslashes and characters after backslashes, and declares the original string. The method is to add'r' or'R' in front of the string.
Note here: the end of line in Python is always'\n', no matter which operating system the Python program is running under. When writing C language under Linux environment, the newline character is'\n', but under windows it is'\r\n'. Write Python programs without worrying about incompatibility caused by different operating environments.
4. User input and formatted output string
The most commonly used functions in Python to get input from the keyboard are raw_input() and input(). But these two functions are quite different:
raw_input() returns everything input by the user in the form of a string;
This is not the case with input(), it will determine the return form according to the form of the input content (may be a bit confusing). Look at an example below to understand:
It is obvious from this example that the difference between the two can be seen. For the same input 123, raw_input() returns the string 123, while input() returns the integer 123. Personally, I suggest using raw_input() to get input in general, so as to avoid unnecessary troubles in the program.
Like the C language, Python also provides formatted output.
The formatted output in Python is similar to the C language, and the basic format is as follows:
print ‘….%formmat..’ %(var…)
When there is only one var, the parentheses can be omitted.
There have been so many discussions about Python strings today, here is only the basic concepts and knowledge points discussed, please refer to the API documentation for the use of related string functions.
The above is the detailed content of strings in Python. For more information about strings in Python, please pay attention to other related articles on ZaLou.Cn!
Recommended Posts