Summary of Python built-in string methods, python strings
String processing is a very common skill, but there are too many built-in string methods in Python that are often forgotten. For quick reference, I wrote examples for each
tests whether an object is a class string
TaskSometimes it is necessary to test an object, especially if you are writing a function or method, you often need to test whether the passed argument is a string.Solution SolutionsUse the built-in isinstance and basestring to quickly and easily query whether an object is a string or a Unicode object method, as follows:Code:def isastring (AAA): return isi
Strings are important data objects in PythonPython strings are any Python data object enclosed in single quotes, double quotes, or three three single quotes, three double quotes, which can be called a python stringNote A data object that contains a single or double quotation mark cannot be wrapped in the middle of a data object (if you need to wrap a line with an escape character \ n) to contain more than o
checks if a character in a character set is included in a string
Task:Checks if a character in a character set is present in the stringSolution:Programme one:ImportItertoolsdefContainany (seq,aset): forIteminchItertools.ifilter (Aset.__contains__, seq):returnTruereturnFalseif __name__=="__main__": L1= List ('python') L2= Set ('x') PrintContainany (L1,L2)Scenario Two:L1 = List ('
elements of the list x to build a dictionary, the key value is the list of each element, value is None4. Get (key) to find value based on key5. Hash_key (x) find if the dictionary contains a key value x6. Update (x) updating another dictionary with one dictionary7, Popitem () delete the last element and return8, Pop (key) delete the key value corresponding to the element9. Items () Converts the dictionary to a list and returns10, Iteritems () Convert dictionary to list, return iterator11. Key (
Python determines whether a string contains a sub-string.
This example describes how to use python to determine whether a string contains a sub-string. Share it with you for your reference. The details are as follows:
The
disadvantage is that it affects performance by stitching strings, modifying strings, and so on. Because it is immutable, it is not a inplace operation to modify a string , to create a new object. This is why it is not recommended to use the + and join ()when stitching multiple strings. Join () calculates the length of all the strings, then copies them all, and then only new objects. You need to be careful of the pits , not all strings will use the i
Conversion between commonly used decimal, hexadecimal, string, and byte string in python (updated for a long time)
During protocol parsing, you will always encounter various data conversion problems, from binary to decimal, from byte string to integer, etc.
Not much nonsense. Let's look at the example directly.
Hexade
filter characters in a string that are not part of a specified collection
Task:Given a collection of strings that need to be preserved, construct a filter function and apply it to any string s, the function returns a copy of S that contains only the elements in the specified character set.Solution: ImportStringallchars= String.maketrans ("',"')#All chars TabledefMakefilter (Keep): Delchars=allchars.t
Python original string and Unicode string operator usage Example Analysis, pythonunicode
This document describes the usage of the original Python string and Unicode string operators. We will share this with you for your reference.
Python string operations (string replacement, deletion, interception, replication, connection, comparison, search, inclusion, Case sensitivity conversion, segmentation, etc.), pythonstring
Remove spaces and special symbols
s.strip().lstrip().rstrip(',')
The Python strip () method is used to remove the specified charact
This article mainly describes the Python Cookbook (string and text) for any number of separators to split the string operation, in conjunction with an instance of the analysis of Python using split () and regular expressions for string splitting operation related implementat
-third-digit charactersPrintstr[:]#truncate all characters of a stringPrintStr[6:]#Intercept the seventh character to the end of thePrintSTR[:-3]#intercept from the beginning to the third of the last characterPrintSTR[2]#interception of the third characterPrintSTR[-1]#Intercept the first character of the countdownPrintSTR[::-1]#create a string opposite to the original string orderPrintSTR[-3:-1]#intercept t
Python string formatting study notes, python Study Notes
In python, the % operator is used to format the output string. The common format is
• Mark the string % value group to be output in the formatThe "format mark
Python string split methodIt is often used. For example, we need to store a long data and store it in a structured way to facilitate data retrieval and processing in the future. Of course, it can be in JSON format. However, data can also be stored in a field and separated by some identifier.
For example, the storage format is:
Format:Name, age | Name of another user, age
Name: Haha, age: 20 | Name:
number (a+bi).
( )
Brackets, same as C language
( )
Four, stringThe string (str) in Python can be enclosed in single or double quotation marks, and single and double quotes can be nested with each other. Examples of official documents are:1>>>'Spam eggs' #Single quotes2 'Spam eggs'3>>>'doesn\ ' t' #Use "to escape " the single quote ...4 "doesn ' t"5>>>"doesn ' t" #
The Template-type string Template usage in the string module of Python, pythontemplate
String. Template ()Add replacement characters in string. Template (), use the "$" symbol, or use the "$ {}" character in the string. Use the
This article mainly introduces the Python character string feature and the simple notes of common string methods. For some basic knowledge in Pyhton getting started, you can refer to single quotes and double quotes to indicate strings. The difference lies in escape.
If you are too lazy to add escape characters, you can add r before the
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.