Python Basic Three

Source: Internet
Author: User

Underlying data type numeric int

The number is mainly used for calculation, there are subtraction and other operations, the following describes a method of use

# bit_length (), converts a decimal number to a binary number of binary digits v = 9= v.bit_length ()print(data)
boolean bool

Boolean value has True and false, corresponding to the true and false of the condition

True is not 0 count

False false 0

Index and slice of string str string

The index of the string, which is the subscript, starts at 0, and the first character from left to right is subscript 0. Strings can also be indexed in reverse order, from right to left, with the first character indexed as-1

A='0123456789'print(a[0])    # output as a character in a string ' 0 'print (a[5])    # output as a character in a string ' 5 'print(a[-1])    # output as a character in a string ' 9 '  Print(a[-6])    # output as a character in a string ' 4 '

A slice is a part of a string that is intercepted by an index, in the form of a variable (beginning: End: Step), forming a new string. Slices can also be reversed, and the step size needs to be added with a minus sign in reverse slices. Slices are taken from the beginning to the end, but do not include the characters corresponding to the trailing index (Guxio regardless of the tail).

A='1234567890'print(a[2:])              # from 3 of index 2 to last, i.e. 34567890 Print (A[1:4])            # from 2 to 4, i.e. 234 Print (a[0:-1:])         # Index to 1 is the character ' 0 ', and the step does not write by default to 1 Print (A[0:-1:2])       # step is 2, output 13579 Print (A[-1:0:-1])      # reverse index, output 098765432
slicesString Common methods
#capitalize: First letter uppercase, other lowercase letters. s='ab CDEFG higk lmn'Print(s.capitalize ()) output result: Ab CDEFG higk LMN#swapcase: Case reversal. s='ab CDEFG higk lmn'Print(s.swapcase ()) output result: AB CDEFG higk LMN#Title: Capitalize the first letter of each word. s='ab CDEFG higk lmn'Print(S.title ()) output result: Ab CDEFG higk LMN#Upper: all uppercase; lower: all lowercase. (Available when judging the captcha)s='ab CDEFG higk lmn'Print(S.upper ())Print(S.lower ()) output result: AB CDEFG higk lmn ab CDEFG higk LMN#Center: Center, padding defaults to spaces. s='ab CDEFG higk lmn'Print(S.center (30,'*'))#extended length and padding characters are enclosed in parentheses. Output result: ******ab CDEFG higk lmn******#Expandtabs: The effect is similar to \ t. #\ t is the default complement of 8-bit characters, or the full-bit 16-bit if it is greater than 8 bits. such as 1234567890\t will output 1234567890, followed by 6 spaces. s='Hqwasdasd\tasdas'Print(S.expandtabs ()) output result: HQWASDASD Asdas#Find () the index through the element, you can find the whole, can slice, find the first character index of the returned element, cannot find the return-1#Index () through the element to look for indexes, can look for the whole, can slice, can't find the errors='ABC DeFgh IGK'Print(S.find ('a'))Print(S.find ('DeF'))Print(S.find ('GK') Output Result: 03-1#StartsWith: Judge the first character; EndsWith: determines the trailing character. Returns True and false. can be sliced. s='ab CDEFG higk lmn'Print(S.startswith ('a'))Print(S.endswith ('LMN'))Print(S.startswith ('C', 3) ) Output result: True true True#Strip: Remove whitespace, line breaks, tab, and so on before and after the string. (To prevent users from entering a space by mistake)s='\tab CDEFG Higk lmn\n'Print(s)Print(S.strip ()) output result: AB CDEFG higk lmn ab CDEFG higk LMN#Split: Converts a string into a list of lists, the default spacer is a space, or you can define other characterss='ab CDEFG higk lmn'T='aaarbbbrcccrddd'Print(S.split ())Print(T.split ('R'))Print(T.split ('R', 1))#Convert onceOutput result: ['AB','CDEFG','Higk','LMN']               ['AAA','BBB','CCC','DDD']               ['AAA','bbbrcccrddd']#Replace : Replaces characters in a string with other characters, definable number of timess='I am a small stream, always forward'Print(S.replace ('Flow','Drip'))Print(S.replace ('Flow','flowing', 1) ) Output: I'm a stream, I'm flowing forever I'm a stream, I'm always moving forward#Isalnum: Determines whether a string consists of letters or numbers. #Isalpha: Determines whether a string consists of only letters. #IsDigit: Determines whether a string consists of only numbers. s='abCD123'Print(S.isalnum ())Print(S.isalpha ())Print(S.isdigit ()) output result: True false False#format: Formatted output#First Kinds ='my name is {}, this year {}, height {}'. Format ('Jinxin', 21,175)Print(s)#The second KindA ='My name is {0}, this year {1}, height {2}, I still call {0}'. Format ('Jinxin', 21,175)Print(a)#Third Kindb ='my name {name}, this year {age}, height {hight}'. Format (name ='Jinxin', hight=175,age=21)Print(b) Output: My name is Jinxin, this year 21, height 175 My name is Jinxin, this year 21, height 175, I still call jinxin my name is Jinxin, this year 21, height 175#count counts the number of occurrences of an elements ='Abababccdda'Print(S.count ('a') ) output results:4#Len: Gets the length of a string or list,tuple, number, etc.s ='123445'Print(Len (s)) output result:6
string Common operationsConversion of data types

int----> str str (int)
STR----> int int (str) can only be a string consisting of numbers

int----> BOOL 0 fasle not 0 True
BOOL----> True T---> 1 F--->0

STR---> bool non null TRUE NULL is FALSE
BOOL----> str str (True) str (False)

Ps:

1, while True:

Pass

2, while 1: #
the same as the dead loop , the execution efficiency is higher than the above high
Pass

Python Basic Three

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.