Python full Stack development class Note _day03

Source: Internet
Author: User
Tags abs ord

Tag:slow   friend    key value    git   sub    asc    Replace string    dea   $$   

What to learn today: 1. Basic data type initial: int: for calculation. STR: Used to store a small amount of data such as: ' Alex ', ' 123 ': Bool:true, Falselist: (list): The inside can put a variety of data, can store a lot of data, easy to manipulate the format of the list is: [' name ', true,[] ...] Tuple: (tuples, also called read-only lists) the format of tuple is: (' name ', true,[] ... Dict: (dictionary): stores a large number of relational data-----------> key values for dict format are: {' name ': ' Old boy ', ' name_list ': [' negative ', ' floret ', ' Little Red ' ...], ' Alex ': {' age ' : +, ' hobby ': ' Old_women ',......}, ...} Set: (set) The format of set is: {' Wusir ', ' Alex ',......} 2.int Explanation: Bit_length () #十进制转化成二进制的最小位数i =5print (I.bit_length ())//3int with str:int<--------&GT;STRSTR---------- The >int int (str) condition is that the string must be all composed of numbers int----------->str str (int) 3.bool detailed: bool and int:bool<---------> Intbool--------->int true----->1 False------>0int---------->boo Nonzero is true, 0 is falsebool with str:bool<--- ------>strbool----------->strstr-------------->bool non-Empty is true (for example: spaces) ' empty string--------->false4.str explained: # STR Knowledge Points: Index, slice, step s= ' Python12 period ' s1=s[0]print (S1)//p slice: Gu Tou regardless of tail forward slice: s2=s[0:5]//pytho---------->s2=s[0:6]//pythons3 =S[:6]//PYTHONS4=S[1:-1]//ython12s5=s[1:]//ython12 period s6=s[:]//python12 period (not s, but copy s) s7=s[:5:2]//pto----------> 1 Take one, 2 is the step size s8=s[4:: 2]//01 period s9=s[2:6:3]//tn-----------> 2 Take one, 3 is a step reverse slice must be followed by a reverse step s10= S[-1:-5:-1]//period 21n#5. Common ways to manipulate strings ①**capitalize ()//Returns a copy of a string, its first character uppercase, and the rest of the lowercase. s= ' Laonanhai ' S1=s.capitalize () print (S1)//laonanhai②*center (length of string, padding)//center, padding is done using the specified padding character (default is ASCII space). If the width is less than or equal to Len (s), the original string is returned. S2=s.center (' $ ')//$$$$ $laoNANhai $$$$$$③***upper ()/lower () Str.upper ()//Returns a copy of a string with all uppercase and lowercase characters converted to upper case. Note Str.upper (). Isupper () may be false S3=s.upper ()//laonanhaistr.lower ()//Returns a copy of a string, all uppercase and lowercase characters are converted to lower case. Note Str.lower (). Islower () may be false s4=s.lower ()//laonanhaiupper ()/lower () Application:  ④***startswith ()/endswith () Str.startswith (' character or string ', "start subscript of the slice," "at the end of the slice)/////If the string prefix starts, returns true; otherwise false. The prefix can also be a tuple of prefixes to find. With an optional start, the test string starts from that position. With an optional end, stop comparing strings in that position.   Note that the start and end strings can be sliced, and the slices are separated by commas s5=s.startswith (' l ')//Trues6=s.startswith (' Lao ')//Trues7=s.startswith (' N ', 3,6)//Truestr.endswith (' character or string ', "start subscript of the slice," subscript "at the end of the slice)//with Str.swithprint (Str.endswith (' on ', 0,6)) #Falseprint ( Str.endswith (' on ', 0,4)) #True ⑤*swapcase ()//Returns a copy of a string where uppercase characters are converted to lowercase and vice versa. Print (Str.swapcase ()) #LAOnanHAI请注意----->s.swapcase (). Swapcase () ==s is not necessarily the right??? Add a bit of new knowledge: Ord () and Hex () ord () #给定一个字符串表示一个Unicode字符, returns an integer representing the Unicode encoding point of the character. For example, Ord (' A ') returns an integer of 97 and Ord (' Euro ') (euro sign) returns 8364. This is the reciprocal of Chr (). Hex () #将一个整数编号转换为用0x前缀的小写十六进制字符串. If x is not a Python int object, you must define the index () method that returns an integer. ⑥*title ()///The first letter of each word that is not separated by letter must be in uppercase print ("They ' re Tom's best friend.") Title ()) #They ' Re Tom's best friend.⑦*** by element Find index ("Str.find", "Start index of Slice," "End index of Slice")// Returns the index of the first occurrence of the string to find in the original string (or within the slice range). If not found, returns-1. str = ' Laonanhai ' Print (Str.find (' A ', 2,5)) #4 (the index value returned is not the index value of the slice but the index value of the original string) print (Str.find (' A ', 2,4)) #- 1 (Find () could not find return-1) str.index (' String ', ' Start index of slice, ' End index of slice ')//Returns the index of the first occurrence of the string to find in the original string (or within the slice range). If not found, it will be an error. str = ' Laonanhai ' Print (Str.index (' A ', 2,4)) #ValueError: Substring not Found⑧***strip (): The main function is to remove the front and back of the string space, line break, tab str = ' \talex\n ' Print (str) # ALEX&NBSP;STR1 = Str.strip () print (STR1) #Alexstrip () One of the applications: strip () Application II: str = ' ABLABAEXSBA ' Print (Str.strip (' a ')) #blabaexSbprint (Str.strip (' abs ')) #labaex扩展出来的两个-----> One is Lstrip () and the other is Rstrip () print (Str.lstrip (' a ')) #blabaexsbaprint ( Str.rstrip (' a ')) #ablabaexsb  print (Str.lstrip (' abs ')) #labaexsbaprint (Str.rstrip (' abs ') #ablabaex ⑨****split (' string used for splitting ', maximum number of splits) default to space split s = ' wusir Alex Taiba ' Print (S.split ()) #[' Wusir ', ' Alex ', ' Taiba ']s = ' Wusir,alex,taiba ' Print (S.split (', ')) #[' Wusir ', ' Alex ', ' Taiba ']s = ' Qwusirqalexqtaiba ' Print (s.split (' Q ')) ' #[', ' Wusir ', ' Alex ', ' Taiba ']s = ' Qwusirqalexqtaiba ' Print (s.split (' Q ', 2)) #[', ' wusir ', ' Alexqtaiba ']⑩*join (an iterative object)---------------> In some cases, Lists (list) can be converted to strings (str) Note: The list-------------->str can only be used with the string s = ' alex ' s1 = ' * '. Join (s) print (S1) #a *l*e*x list_of_s = [ ' Alex ', ' Wusir ', ' taibai ']s = '. Join (list_of_s) print (S,type (s)) Alex Wusir taibai <class ' str ' >?repalce (' replaced string ', ' substituted string ', ' the number of times the substituted string needs to be replaced ')    #6. Common common methods for basic data types ①len (strings, Bytes, tuples, lists, or collections (such as dictionaries)--------------------> Total number of S = ' SSFJASKHJKAHE987#%2SL ' Print (len (s)) #21 ②count (' String to count occurrences ', start of slice, end of Slice "")----------------> calculates the number of occurrences of some elements, can be sliced s = ' s2sfjaskhjkahe987#%2sl ' Print (S.count (' J ', 2,-4)) #2 ③format () formatted output:  ④isalnum ()---------------------------> whether the string is composed of letters or numbers. Returns true if all characters in the string are alphanumeric and have at least one character. ⑤isalpha ()---------------------> whether the string is composed only of letters. Returns true if all characters in the string are sorted alphabetically and have at least one character. ⑥isdigit ()-------------------------> Strings consist only of numbers. Returns true if all characters in the string are numeric and have at least one character. Formally, a number is a number that has an attribute value = numeric or numeric = decimal character. s = ' jinxin123 ' Print (S.isalnum ()) #Trueprint (S.isalpha ()) #Falseprint (S.isdigit ()) #False. The For loop outputs each character of a string.  

Python full Stack development class Note _day03

Related Article

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.