Python base continuation (base data type)

Source: Internet
Author: User

A holistic initial knowledge data type

1.1int

The number is mainly used for computing, not many ways to use, just remember one can:

# bit_length () when decimal is represented by a binary, the minimum number of bits i = 4print= oneprint3, 4     

1.2bool

There are two types of Boolean values: True,false. is the correct condition of the reaction.

True 1 true.

False 0 false.

1.3str

1.3.1, the index of the string, and the slice.

The index is subscript, that is, the elements of the string begin with the first, the initial index is 0, and so on.

 s =  " abcdefghijklmn  "  print  (S[0]) #  Span style= "COLOR: #008000" > output result a  print  (s[1]) #   output result B  print  (S[5]) #   output result F  print          (S[8]) #   output result I  print          (S[-1]) #   output result n  print          (S[-3]) #   output result L  

A slice is a paragraph that intercepts a string by index (index: Index: STRIDE), forming a new string (the principle is that the head ignores the end)

s ='ABCDEFGHIJKLMN'Print(S[0:7])#output Result: ABCDEFGPrint(S[1:7])#output Result: BCDEFGPrint(s[0:])#output Result: ABCDEFGHIJKLMNPrint(S[5:0:-1])#-1 Reverse plus step output result: FEDCBPrint(S[-1:-5:-1])#the first-1 is the last, the second-1 reverse plus step output result: NmlkPrint(S[-1:-5:-2])#-2 Reverse plus step output result: NL

1.3.2, String Common methods

*capitalize () capital letter, other letters lowercase
s =  'Alex Wusir'print(S.capitalize ())                  #  output: Alex Wusir 
Capitalize
*swapcase () Case reversal
s =  'Alex Wusir'print(S.swapcase ())                  #  Output result: ALEX Wusir
Swapcase
*title () non-letter-separated sections, uppercase, other lowercase
s =  'Alex Wusir1taibai*ritian'print(S.title ())                    #  Output result: Alex Wusir1taibai*ritian
title
Upper ()  all caps
Lower () all lowercase
s =  'alexawusir'print(S.upper ())              #  Output: AlexawusirPrint(S.lower ())              # output: Alexawusir
Upper Lower

Examples used in the CAPTCHA:

' AbCD '  = input (' Please enter your verification code:')if you_code.upper () = = Code.upper ():     Print (' correct ') Else :     Print (' please re-enter ')
Application of Upper
*center (,) centered on what, padding default empty
s =  'Alex Wusir'print(s.center)         # output:     Alex Wusir     print(s.center,'*')    # output Result: Alex wusir*****print(S.center (1))         # output: Alex Wusir
Center (,)
Expandtabs ()

\ t Front of completion
By default, a tab key becomes 8 spaces, if the character in front of the tab is less than 8, then complete 8, if the character in front of the TAB key is more than 8 less than 16, then complete 16, and so on each complement of 8.
s =  'al\tex wusir'print(s.expandtabs ()) output: Al      ex Wusir
Expandtabs
Find () The index through the element, can be found in the whole, can be sliced, cannot find return-1
Index () through the element to look for indexes, can look for the whole, can slice, can't find the error
s ='ALEXSS dhfgsdjddfdf'Print(S.find ('a'), type (S.find ('a')))Print(S.find ('Alex'), type (S.find ('a')))Print(S.find ('a'))Print(S.find ('a', 1,5))Print(S.find ('L') Output Result: 0<class 'int'>0<class 'int'>0-1-1
Find ()
StartsWith  EndsWith
s ='Alex Wusir'Print(S.startswith ('a'))Print(S.startswith ('Al'))Print(S.startswith ('W', 5))Print(S.startswith ('W', 5))Print(S.endswith ('R'))Print(S.endswith ('R',-1))Print(S.endswith ('u',-4)) Output result: Truetruetruefalsetruetruefalse
startswith EndsWith
Strip () remove spaces, line breaks, tab keys, and so on at both ends of a string
If there is content within (), remove the content, see example
s =  '\talex wusir\n'alalelllllllxwusirbl' Print (S.strip ()) Print (S.lstrip ()) Print (S.rstrip ()) Print (S1.strip ('lab')) output: Alex Wusiralex wusir    Alex Wusirelllllllxwusir 
Strip

Strip Application at Login

Name = input (' Please enter name:'). Strip ()if'Alex' :     Print ('somebody') else:print(' please re-enter ')
Strip
Split str--->list method
s ='Alex;wusir;ritian'S1='Alexalaria'Print(S.split (';'))Print(S1.split ('a'))Print(S1.split ('a', 1) output Result: ['Alex','Wusir','Ritian']["','Lex','L','RI',"']["','Lexalaria']
Split
Replace
' brother and sister , old boy, old boy. '  = s1.replace (' old ',' small '= S1.replace (  ' old ',' small ', 1)print(S2)  Print(S3) output: Sister and brother, Little boy, Little boy, brother and sister, young boy, old boy.
Replace
Isalnum () #字符串由字母或数字组成
Isalpha () #字符串只由字母组成
IsDigit () #字符串只由数字组成
Name='jinxin123'print# string composed of letters or numbers print# A string consists only of letters the print# string is only composed of numbers output: Truefalsefalse
is
Format
s ='I am {} , this year {} years old, height {}cm'. Format ('people', 18,175)Print(s) output: I am a man, 18 years old, height 175cms='I am {0}, this year {1} years old, height {2}cm,{0} The will of the People'. Format ('people', 18,175)Print(s) Output: I am human, 18 years old, height 175cm, people's wishes s='I am {name}, this year {age}, height {high}cm'. Format (name ='people', age = in, high =175)Print(s) output: I am a man, 18 years old, height 175cm
three formatted outputs of format
Count counts the number of occurrences of an element
s =  'alexaaaaa wusir'print(S.count ('a' ) 6
usage of Count
Len  computes the number of string bits
' Alex '   'print(len (s))print(len (S1) ) Output:412
Len

Two: For loop

The user iterates through the contents of an object in order.

Example: Print the elements in a string sequentially

' Salffdsafdsag '  0 while Count < Len (s):    print (S[count])    1# Method for loop: for in s:    print (i)
usage of the For loop

Python base continuation (base data type)

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.