Summary of functions and methods for sharing common operation strings in Python

Source: Internet
Author: User
Tags string methods
This article mainly shares Python common operation string function and method summary knot, including the string format output and splicing and other basic knowledge, need friends can refer to the next





For example, a string Python, which is just a few characters: P,y,t,h,o,n, is lined up. This arrangement is very strict, not only the character itself, but also the order, in other words, if a character is changed, a new string is programmed, and if the order of these characters changes, it becomes a new string.



In Python, an object type such as a string (followed by a similar object type, such as a list), is referred to as a sequence. As the name implies, sequences are "ordered".



For example, the water park Liangshan 108 Heroes (There are clear there are women, is the female man from here?) ) is a sequence of "ordered permutations". From Song to the 108th Golden Dog segment. In this sequence, each person has a number, and the number corresponds to one by one of each person. Number 1th is Song Jiang, number 2nd is Lu Junyi. In turn, by each person's name, can also find his corresponding number. What number is Wu song? Number 14th. Where's Li Kui? Number 22nd.



In Python, these numbers are given an elegant name, called an index (which is what other programming languages call it, not Python-exclusive.) )。



Indexes and slices
In front of the Liangshan heroes as an example to illustrate the index. Let's look at the example in Python:





>>> lang = "study Python"
>>> lang[0]
's'
>>> lang[1]
't'





Has a string that is assigned to the variable lang by an assignment statement. If you want to get the first word of this string s, you can use lang[0]. Of course, if you don't want to pass an assignment statement, let the variable lang point to that string, you can do the following:





>>> "study Python" [0] ' s '





The effect is the same. Because Lang is a label, it points to the "study Python" string. When you let Python execute lang[0], it's going to go to that string object, just like the one above. Only, if you do not use lang such a variable, if you write again, the cost of ink, to each time the string to write the whole. To save it, copy it to a variable. A variable is a representation of a string.



The sequence of the string is a bit different from the Liangshan heroes, the first is not represented by the number 1, but by the number 0. Not only Python, but many other languages have been sorted from 0. Why did you do it? That's the rule. Of course, there is a certain advantage to this rule. Do not expand here, interested in the Internet to Google, there is a special article to explain this.





0 1 2 3 4 5 6 7 8 9 10 11< /th>
S T U D Y L P Y T H O N



In the table above, the string is sorted from the first to the last, paying special attention to the space in the middle of the two words, which also takes up a position.



The index is able to find the corresponding character of the index, then, in turn, can you find its index value in the string by character? How to find?





>>> lang.index ("P") 6





In this way, is not already able to and Liangshan heroes of the example of the number? Only the difference is that the index value of the first one is 0.



If one day, brother song stood on the Big stone, and shouted to all the brothers: "Brothers, all lined up." After the brothers lined up, Song Jiang said: "Now you have no wife brother assigned girlfriend, I have a list here, I talk about the brothers stand out." But I read it according to the serial number. 29th to 34th, out of the queue, to the next house waiting to assign a girlfriend. ”



In the previous example, Lang[1] can get the second character T of the original string, which is equivalent to "cut" it out of the original string. However, we are so "cut" without affecting the integrity of the original string, of course, it can be understood that the character T is assigned to the value of a copy taken out.



Then Song Jiang Brother did not have a "cut", but a few brothers called out. You can do similar things in Python.





> > > lang
After "cutting" a few characters, look at the string again and see that it is still complete.
> > > lang [5]
'udy pyt'





by Lang[2:9] to get a partial (not a) character, as you can see from the returned results, we get the serial number corresponding to the 2,3,4,5,6,7,8 (corresponding to the table above) character (including that space). That is, this method of obtaining a partial character is able to obtain the corresponding character that is required by the beginning and before the last ordinal. A bit of a mouthful, you can control the number of the above table to know. To put it simply is to include the beginning, not including the end.



The process of getting a character through an index, whether it's getting one or more, is called slicing.



Slicing is a very interesting thing. Can "cut" out a lot of tricks?





> > > lang
'study Python'
>, >, >, b = lang[1:] # gets the character from number 1 to the very end, when the last one needs not to be written
> > > b
'tudy Python'
>>> c = lang[:] # gets all characters
> > > c
'study Python'
>>> d = lang[:10] # gets the character from the first to the 10th
> > > d
'study pyth'





When you get a slice, if the preceding or subsequent sequence number of the semicolon is not written, it is either to the last (not the back) or the first (not the previous one)



LANG[:10] is the same as the effect of lang[0:10].





>>> e = lang[0:10]>>> E ' study Pyth '





So, lang[1:] The same as the lang[1:11] effect? Please think after you answer.





>>> lang[1:11] ' tudy pytho ' >>> lang[1:] ' tudy python '





Sure enough, do you think it right? The reason for this is that if there is a number after the semicolon, the resulting slice does not contain the ordinal number (formerly included, not included) of the number. So, is it possible? Lang[1:12], does not include number 12th (fact no 12th), is it possible to get 1 to 11th corresponding characters?





>>> Lang[1:12] ' tudy python ' >>> lang[1:13] ' tudy python '





It sure is. And not only to write 12, write 13, but also to get the same result. However, I would like to remind you that this approach to slicing is not advocated in programming practice. Especially if the loop is used later, this might be a problem at some point.



If at the time of slicing, the colon does not write the number, is the previous operation C = lang[:], the result is that the value of the variable C is the same as the original string, that is, "copy" a copy. Note that the "copy" here I have quoted, meaning like copy, is not really copy it? You can test it in the following way





>>> ID (c) 3071934536l>>> id (lang) 3071934536L





The function of ID () is to look at the memory address of the object (that is, the location number in memory). As can be seen from the above, the two memory address is the same, indicating that the C and Lang Two variables point to the same object. In the way of c=lang[:], and did not generate a new string, but the variable C is also affixed to the label on the original string.





>>> lang = "study python" >>> c = lang





If so, do the variables C and Lang point to the same object? Or what is the memory address of the object that the two points to? Crossing can be viewed on its own.



String Basic operation
A string is a sequence in which all sequences have the following basic operations:


    • Len (): Find sequence length

    • : Connect 2 sequences

    • : Repeating sequence elements

    • In: Determines whether an element exists in the sequence

    • Max (): Returns the maximum value

    • MIN (): Returns the minimum value

    • CMP (STR1,STR2): Compare 2 Series values


The following example illustrates the use of these basic operations on strings:



"+" Connection string





>>> str1 + str2 ' abcdabcde ' >>> str1 + "+" + str2 ' ABCD-->ABCDE '





This is actually splicing, but here, crossing should have a bigger idea, we now just learn the string of this kind of sequence, we will also encounter the list, tuple two sequences, can be so realized splicing.



Inch





>>> ' A ' in str1true>>> ' de ' in str1false>>> ' de ' in str2true





In is used to determine if a string is within another string, or whether a string is contained within a character, and if so, returns True, otherwise False.



Best Value





>>> Max (str1) ' d ' >>> max (str2) ' E ' >>> min (str1) ' a '





In a string, each character is encoded within the computer, which corresponds to a number, and Min () and Max () are given the minimum and maximum values in this number and correspond to the corresponding characters. About this number, crossing can be Google about character encoding, or ASCII encoding what, it is easy to find.



Comparison





>>> CMP (STR1, STR2)-1





Comparing two strings is also the first to convert the symbols in the string to a number on one, and then compare them. If the returned value is less than 0, the first one is less than the second, equals 0, then two equals, greater than 0, and the first is greater than the second. To be able to understand why, enter the following analysis.





>>> Ord (' a ') 97>>> ord (' B ') 98>>> ord (') 32





Ord () is an intrinsic function that is able to return a character (note that it is a character, not a string consisting of multiple characters) on an ASCII value (which is decimal), the value of character A in ASCII is 97, and the space in ASCII also has a value, which is 32. By the way, in turn, the corresponding characters are obtained according to integer values, and Chr () can be used:





>>> chr ' a ' >>> chr (98) ' B '





As a result, the following comparisons have been made:





> > > CMP (" a ", "b") -- - > 97 # a, b - > 98, 97 less than 98, so a less than b
- 1
> > > CMP (" ABC ", "aaa")
1
> > > CMP (" a ", "a")
0





Look at the comparison below, how does it proceed?





>>> CMP ("Ad", "C")-1





In a string comparison, the first character of the two string is Mr. Foo compared, and if it is equal, the next is compared, and if not equal, the result is returned. Until finally, if it is equal, it returns 0. When the number of digits is not enough, follow no processing (note that no 0,0 in ASCII corresponds to the NUL), the number of bits of the natural big. In AD, A is compared with the following C, obviously a is less than C, so it returns the result-1. If you make the following comparisons, it is the easiest to confuse people. Can crossing be far from understanding according to the comparison just described?





> > > CMP (" 123 ", "23")
- 1
>, >, >, CMP (123,23) # can also be used to compare integers.
1
"*"





"Multiplication" in a string, the multiplication, is the meaning of repeating that string. It's very useful at some point. For example, I want to print a gorgeous split line:





>>> str1*3 ' ABCDABCDABCD ' >>> print "-" *20  # Don't enter a lot of '-'--------------------len ()





To know how many characters a string has, one way is to start from scratch and stare at the number of screens. Oh, this is not the computer in the work, is the key guest in the work.



Key guest, not swordsman. The swordsman is a knight with the sword as its weapon, while the key guest is a knight with the keyboard as its weapon. Of course, there are also cheap, that is the highest level of the base, cheap to the degree of the warrior, such as Yeu Bu.
Key to the number of characters such as String length:





>>> a= "Hello" >>> Len (a) 5





A function Len (object) is used. The resulting result is the length of the string.





>>> m = Len (a) # assigns a value to a variable after returning the result >>> m5>>> type (m)   # This return value (variable) is an integer type <type ' int ' >





String formatted output
What is formatting? There are specific entries in Wikipedia, so to speak:



Formatting is an operation that initializes a partition (partition) on a disk or disk, which typically causes all files in an existing disk or partition to be purged.
I don't know if you know this kind of "formatting". Obviously, this format is not what we say here, we are talking about the format of the string, or "formatted string", can be, means:



A formatted string is a string parameter that specifies the format and relative position of the output parameter in the printf class function in C, C + +, and other programming languages. The conversion instructions (conversion specification) are used to convert the corresponding 0 or more function parameters to the corresponding format output; the characters in the formatted string other than the conversion description are output as-is.
This is also a definition from Wikipedia. In this definition, the C language is used as an example, and its output function is used to illustrate. In Python, there is the same operation and similar function print, which we already know about twos.



If you make that definition a bit more popular, the string format is to make a template that leaves empty space in one or several places in the template, and then fills in those blanks with strings. So, those slots, which need to be represented by a symbol, are often called placeholders (which occupy only that position, not the output).





>>> "I like%s" ' I like%s '





In this string, there is a symbol:%s, which is a placeholder that can be replaced by other strings. Like what:





>>> "I like%s"% "Python" ' I like Python ' >>> "I like%s"% "Pascal" ' I like Pascal '





This is a more commonly used method of string output.



In addition, different placeholders indicate that the position should be populated with different types of objects. Many are listed below for reference. However, no memory, the usual only%s and%d, or add%f, the other if necessary, come here to check.





placeholder Description
%s String (shown with STR ())
%r String (shown with repr ())
%c Single character
%b binary integers
%d Decimal integer
%i Decimal integer
%o Eight-binary integers
%x hexadecimal integer
%e Index (base written as E)
%E Index (base written as E)
%f Floating point number
%F Floating point number, same as above
%g Index (e) or floating-point number (depending on the display length)
%G Index (E) or floating-point number (depending on the display length)



See Example:





>>> a = "%d years"% 15>>> print A15 years





Of course, you can also set multiple placeholders in a string, just like the following





>>> print "Suzhou is more than%d years. %s lives in here. "% (2500," Qiwsir ") Suzhou are more than 2500 years. Qiwsir lives in here.





For print output of floating-point numbers, you can also limit the number of decimal digits and other styles of the output.





>>> print "Today's temperature is%.2f"% 12.235Today ' s temperature is 12.23>>> print "Today's Temperatu Re is%+.2f "% 12.235Today ' s temperature is +12.23





Note that the above example does not implement rounding operations. Just intercept.



Common string methods
There are many ways to string strings. Can be viewed via dir:





>>> dir(str)
['add', 'class', 'contains', 'delattr', 'doc', 'eq', 'format', 'ge', 'getattribute', 'getitem', 'getnewargs', 'getslice', 'gt', 'hash', 'init', 'le', 'len', 'lt', 'mod', 'mul', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'rmod', 'rmul', 'setattr', 'sizeof', 'str', 'subclasshook', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']





So many, will not be introduced, to understand a specific meaning and use, it is best to use Help to view. Example:





>>> Help (Str.isalpha)







Help on Method_descriptor:isalpha (...)  S.isalpha (), bool  Return True If all characters in S was alphabetic  and there is at least one character in S, False otherwise.





As described here, you can experiment in interactive mode.





>>> "Python". Isalpha ()  # strings are all letters and should return truetrue>>> "2python". Isalpha ()  # String with non-letter, return Falsefalse





Split



The purpose of this function is to divide a string into a separator.





>>> a = "I love PYTHON" >>> a.split ("") [' I ', ' love ', ' python ']





This is separated by a space, get a name called List of the return value, about the contents of the list, followed by the introduction. Can you use any other separation?





>>> B = "www.itdiffer.com" >>> b.split (".") [' www ', ' itdiffer ', ' com ']





Remove spaces at both ends of a string



This feature is useful when you want to let users enter some information. Some friends like to enter the end of the time to tap the space, such as let him enter his name, lost, he came a space. Some prefer to add a space first, the total input of the first word should be empty before two grid.



These spaces are useless. Python takes into account that many people may have this habit, so it helps programmers to remove these spaces.



The method is:


    1. S.strip () Remove the left and right spaces of the string

    2. S.lstrip () Remove the left space of the string

    3. S.rstrip () Remove the right space of the string

    4. For example:




>>> b= "Hello"  # with spaces on both sides >>> B.strip () ' Hello ' >>> b ' Hello '





Pay special attention to the fact that the original value has not changed, but that a new result is returned.





>>> B.lstrip ()  # Remove the left space ' Hello ' >>> b.rstrip ()  # Remove the space on the right ' hello '





Character-Case conversions



For English, it is sometimes used to convert to uppercase and lowercase. Named after the most famous hump, there are some uppercase and lowercase participating. If you are interested, you can come here to see how to automatically convert a string into a camel-named form.



In Python, there are a bunch of built-in functions to implement various types of case conversions


    • S.upper () uppercase letters in the #S

    • S.lower () lowercase letters in the #S

    • S.capitalize () # Capitalize first letter

    • S.isupper () whether the letters in the #S are all uppercase

    • S.islower () whether the letters in the #S are all lowercase

    • S.istitle ()

    • See Example:




>>> a = "Qiwsir,python" >>> a.upper () # turns the lowercase letter into uppercase ' Qiwsir,python ' >>> A # The original data object is not changed Change ' Qiwsir,python ' >>> b = A.upper () >>> B ' Qiwsir,python ' >>> c = b.lower () # turn all lowercase letters into uppercase letters >& gt;> c ' Qiwsir,python ' >>> a ' Qiwsir,python ' >>> a.capitalize () # Turn the first letter of a string into uppercase ' Qiwsir,python ' >>> a # Original data object did not change ' Qiwsir,python ' >>> b = a.capitalize () # New Set up a >>> B ' Qiwsir,python ' >& Gt;> a = "Qiwsir,github" # Here's the problem is the netizen white Feather pointed out, thank him very much.  >>> A.istitle () false>>> a = "Qiwsir" # when all uppercase, return false>>> A.istitle () false>>> a = "Qiwsir" >>> a.istitle () false>>> a = "Qiwsir,github" # If so, also return false>>> a.istitle () False >>> a = "Qiwsir" # This is true>>> a.istitle () true>>> a = ' Qiwsir,github ' # This is also True>>&gt ; A.istitle () true>>> a = "Qiwsir" >>> a.isupper () false>>> a.upper (). Isupper () true>>> a.islower () false>>> a.lower (). Islower () True 





To explore, you can do this:





>>> A = "This was a book" >>> A.istitle () false>>> B = A.title ()   # so that the first letter of all words is converted to uppercase >> ;> B ' This was A book ' >>> B.istitle ()    # Determines whether the first letter of each word is uppercase true





Join concatenation string



"+" can be used to stitch strings, but not under any circumstances can be desired. For example, the list (about the list, later in detail, it is another type) in each character (string) elements are stitched into a string, and with a symbolic connection, if the "+", it is more troublesome (can be achieved, trouble).



Joins with strings are easier to implement.





>>> b ' www.itdiffer.com ' >>> c = b.split (".") >>> c[' www ', ' itdiffer ', ' com ']>>> '. Join (c) ' www.itdiffer.com ' >>> ' * '. Join (c) ' www*itdiffer*com '






This splicing, is not simple?


Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.