This article mainly introduces a concise summary of Python string processing functions. This article summarizes some common string processing functions. For more information, see
Returns the string with the specified characters removed.
Blank characters are removed by default.
Delete the first and last characters:
Python format function, python string format
1. The format can accept infinite parameters, and the positions can be unordered:
In [1]: "{}{}". format ("hello", "world") # do not set the location, In the default order Out [1]: 'Hello World' In [2]: "{0} {1 }". format ("hello", "world") # specify the location Out [2]: 'Hello World' In [3]: "{1} {0} {1 }". format ("
strings:S[::-1]Reverse the word only:Import reRev = Re.split (R ' (\s+) ', ' Hello World hahaha! ‘)Rev.reverse ()Rev = '. Join (REV) #要用空字符来join because the space is already in the cut listReturns all elements in a that are not part of B:Set (a). Difference (set (b))Translate method:Import strings = ' Hello world! 'Table = String.maketrans (' abcde ', ' 12345 ') #先生成对照表S.translate (table) #依照对照表进行替换操作S.translate (table, ' wor ') #替换后, delete characters containing worIf you want to delete only,
This article describes how to use a regular expression to match and intercept the specified substring and deduplicate in Python. It involves the skills related to Python Regular Expression matching and string truncation operations and has some reference value, for more information, see the following example. Share it with you for your reference. The details are a
sequences together* Elements of a repeating sequenceIn determining whether an element is in a sequenceMax returns the maximum value of a sequenceMin Returns the minimum value of the sequenceCMP (STR1,STR2) compares two series values for equalityExample:1>>> A ="Hello World"2>>>"Hello"+"-----"+" World"#连接字符串3 'Hello----- World'4>>>Len (a) #计算序列的长度5116>>>Print "-"*100 #减号字符串重复100 Gorgeous Split-line7--------------------------------------------------------------------------------------------------
to the character encoding, for example, the letter A-Z can be expressed in ASCII code (occupies one byte), can also be expressed in UNICODE (occupies two bytes ), it can also be expressed in UTF-8 (occupies one byte ). Character encoding is used to convert human-identifiable characters into machine-identifiable bytecode and reverse processes.
UNICDOE is the real string, and the byte string is encoded with
understanding is not so clear. If we know what the effect of the default encoding is, we will understand the meaning of that phrase more clearly.
Ii. Related Concepts
1. Characters and bytes
A character is not equivalent to a byte, a character is a symbol that human beings can recognize, and these symbols need to be stored in the computed storage to be represented by a byte that the computer can recognize. A character often has multiple representations, and different representations use a di
.
Unicdoe is the real string, and the ASCII, UTF-8, GBK and other character encodings represent the byte string . In this regard, we can often see in Python's official documentation The description "Unicode string", "translating a Unicode string into a sequence of bytes"
We write code that is written in a file, and the
bytecode and reverse processes.Unicdoe is the real string, and the ASCII, UTF-8, GBK and other character encodings represent the byte string . In this regard, we can often see in Python's official documentation The description "Unicode string", "translating a Unicode string into a sequence of bytes"We write code that
PythonstringThe string is the most commonly used data type in Python. We can use quotation marks to create strings.Creating a string is simple, as long as you assign a value to the variable. For example:=' Hello world! ' ="Python programming"
Python accesses a value
Python string details
This article mainly introduces the details of Python strings. This article describes the knowledge about strings, some features of strings, the original strings, unicode strings, common operation methods of strings, and the list of built-in functions, for more information, see
Introduction
String
Problem: Using Python to match strings between two strings in a regular wayWorkaround: Using the FindAll of the RE module, note that Re.match is only matched from the beginningMethod:Import''= Re.findall (R'', html_str)print(local[0]) #' var, FDS, FDS, FDs FDS, FDS fds Locafds; 'So that you can match the code in the script tag.Python matches two strings in the middle of a
In Python, the type of a string can be categorized as Class three: Str,bytes,bytearray, where str and bytes are immutable types, and ByteArray are mutable types. Because of the difference between Python 2.X and Python 3.X, they have different meanings in Python 2.X and
Describes how to implement a Python String object,
PyStringObject struct
The string object in Python corresponds to a struct called PyStringObject internally. "Ob_shash" corresponds to the hash value calculated by the string. "ob_sval" points to a
Python escape characterPython uses a backslash (\) to escape characters when special characters are needed in characters. As the following table:
Escape Character
Description
\ (at end of line)
Line continuation character
\\
Backslash symbol
\‘
Single quotation marks
\"
Double quotes
\a
Bell
\b
BACKSPACE
The string is the most commonly used data type in Python. We can use quotation marks (' or ') to create a string.Creating a string is simple, as long as you assign a value to the variable. Var1 = ' Hello world! ' var2 = "Python runoob" Python accesses a value in a stri
A string sequence is used to represent and store text, and a string in Python is an immutable object. A string is a collection of ordered characters that is used to store and represent basic textual information, a pair of single, double, or triple quotation marks that are contained in the middle of what is called a
String typeIn Python, string types can be used in pairs of single quotes, such as: ' Hello, China. can also be used in pairs of double quotes, such as: "Bad, I drop country." The code is as follows: Through. Title (), you can turn the first letter of a paragraph into uppercase letters, such as: "I love you!" Use. Upper () and. Lower () to make the
In this blog, "Python string series," The following is covered:
Python built-in STR objects and operations
Formatting of strings
Unicode strings in Python
Regular expressions in Python
Re module
This article describes Python's built-in STR types
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.