This article mainly introduces Python string details, this article explains the knowledge of string, some characters of string, raw string, Unicode string, common operation method of string
A string is one of the most commonly used data types in Python, and the essence of a string is a value, just like a numberIt is also easy to create a string by assigning a value to the variableThe value of the note is that the string is not variable and cannot be modifiedAll
The string type is a very important type in Python and is usually expressed in the form of a character in the middle of a quotation mark, unlike in other languages, where the double quotation marks ("") in Python are not distinguished from single quotation marks (""). Can be used to represent a stringCreate, assign, and access 1. Two ways of creating(1) Direct as
1. Single-quote strings and escape charactersstrings, like numbers, are values that can be used directly, entering strings directly in the Python console, such as "Hello World," and outputting the string as is, except in single quotes.>>> "Hello World"‘Hello World‘So what's the difference between a string enclosed in double and single quotes? In fact, there is no
A method of connecting two strings is already mentioned in the previous chapter. Review:
Copy Code code as follows:
>>> a= ' py '
>>> b= ' Thon '
>>> a+b
' Python '
Since this is a method, by implication, there is another way.
Method of connection String 2
Before saying Method 2, explain what is a placeholder, before explaining the variable (parameter), mentioned the placeh
Python is simple, free, open-source, high-level language, portability, explanatory, object-oriented, scalable, embedded, and rich library, python is indeed a wonderful and powerful language. It reasonably combines high performance and makes programming simple and interesting. The following describes the python string o
Share the key points of the Python string,
A string is the most common data type in Python. We can use quotation marks to create strings. The key points of a python string are as follows:
1. Some characters separated by quotation
The Python string string is a common data type in Pyhton, and we can use quotation marks to create a string. It's easy to create a string, not to mention it. Python accesses the value in the s
newline, \ t for the tab, the character \ itself to escape, so \ \ means the character is \, you can print the string in Python's interactive command line () to see:> >> print (' I\ ' mOk. ') I ' m ok.>>> print (' I\ ' m learning\npython. ') I ' m learningpython.>>> print (' \\\n\\ ') \ \ \ \ If there are many characters in the string that need to be escaped, you need to add a lot of \, in order to simplif
String formatting:
The code is as follows
Copy Code
format = "Hello%s,%s enough for ya?"Values = (' World ', ' hot ')Print format% values
Result: Hello world, hot enough for ya?Note: If not executed at the command line, enclose the print behind parentheses
Similar to PHP but not the same function or method name:
explode/"target=" _blank ">php explode=> python splitPH
1. String concatenation2. Formatted output3. God copy and shallow copy1. String concatenationExample: a= ' Hello ', b= ' python ', c= '! ' Connect the strings in the a,b,c to a single sentence. 1. A+b+c2 with + number. Format string% '%s%s '% (a,b,c) 3. '. Join () method, note that the brackets are to be connected (can
10111000 10101101 from the table above can also be found, One additional benefit of UTF-8 encoding is that ASCII encoding can actually be seen as part of the UTF-8 encoding, so a large number of legacy software that only supports ASCII encoding can continue to work under UTF-8 encoding.Figuring out the relationship between ASCII, Unicode, and UTF-8, we can summarize how the current computer system works with character encoding:In computer memory, Unicode encoding is used uniformly, and is conve
right side. You can use a value, such as a string or number:
>>> Print "Hello, % s! "% 'Signature'
Hello, signjing!
>>> Age = 20
>>> Print "I'm % d years old! "% Age
I'm 20 years old!
You can also use tuples or dictionaries with multiple values. Use tuples in general:
>>> Format = "Hello, % s, % s enough for ya? "
>>> Values = ('World', 'hot ')
>>> Print format % values
Hello, world, Hot enough for ya?
If you use a list or another sequence instead
string and text manipulation strings:
The biggest difference between Python 2 and Python 3 is that the string
The string in Python 2 is an ordered sequence of byte
The string
length_of_persons_name.
Use the lowercase l and the capital Letter o sparingly, as they may be mistaken for numbers 1 and 0.
Creating good variable names requires a certain amount of practice, especially when the program is complex and interesting. As you write more and more programs, and start reading code written by others, you'll be more adept at creating meaningful variable names.Note: For now, you should use the lowercase python variabl
;>> Import re>>> M = Re. Search ('\ [rtfvn]', r'hello world! \ N ')>>> If M is not none: M. Group ()...>>> M = Re. Search (R' \ [rtfvn] ', r'hello world! \ N ')>>> If M is not none: M. Group ()...'\ N'
Role of 'R' in Regular Expressions:
Core notes: usage of the python raw string (raw strings)You may have seen some examples of the original string usage. The orig
here means that the string followed is stored in Unicode format. Python will identify the kanji ' ha ' in the code according to the Utf-8 encoding, which is the first line of code, and then convert it to a Unicode object. If we look at the data class of ' ha ' with typeType type (' Ha '), will get , which indicates that this is just a string using some encoding,
Python built-in data operations (1) --- string str, pythonstr
S. capitalize ()-> str Return a capitalized version of S, I. e. make the first character have upper case and the rest lower case.Returns an upper-case string that converts the first letter of the string to the upper-case
Python supports the output of formatted strings. Although this may use a very complex expression, the most basic usage is to insert a value into a string that has the string format of%s.Shanghai Python Training-Shanghai Shang Academy editorial writing.In the age of artificial intelligence,
String Type (str)
String description
Escape character
String operators
String formatting
Functions built into strings
I. String descriptionThe string is the most commonly used data type in
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.