Python Basic data type

Source: Internet
Author: User

First, the basic data type: 1, int:
S=4s1=s.bit_length () print (S1)
2, BOOL:
There are two types of Boolean values: True,false. is the correct condition of the reaction. True   1   true. False   0   false.    
3, str:1) index and slice

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

A= ' Lh_like_football ' S1=a[0]s2=a[1]s3=a[2]print (S1)

A slice is a section of a string that is intercepted by an index (index: Index: STRIDE), forming a new string (the principle is that Gu head ignores butt).

A = ' Abcdefghijk ' Print (A[0:3]) print (A[2:5]) print (a[0:]) #默认到最后print (a[0:-1]) #-1 is the last print (A[0:5:2]) #加步长print (a[ 5:0:-2]) #反向加步长

2) string common methods.

First letter uppercase, other lowercase letters:

s= ' Luhanlike ' S1=s.capitalize () print (S1)

All uppercase. All lowercase

S1=s.upper () S2=s.lower ()

Center Center:

s= ' Luhan ' S1=s.center (S1)               #输出      luhans2=s.center (' * ') print (s2)               #输出 **luhan**

Case Flip:

S3=s.swapcase () print (S3)                 #输出LUHAN
The first letter of each word is capitalized (not separated by letters):
s= ' Luhan like*football ' S4=s.title () print (S4)                #输出Luhan Like*football
Decide what to start with and what to end with.
s= ' Luhan like*football ' S5=s.startswith (' U ') print (S5)                      #输出False
Remove leading and trailing spaces, line breaks, tab
Remove left space, line break, Tab   Lstrip ()
Remove the right space, line break, Tab Rstrip ()
s= ' \nluhan like*football\t '         #\n is a newline, \ t is a space print (S.strip ())               #输出luhan Like*footballprint (S.lstrip ())               # Output Luhan like*football+ space print (S.rstrip ())               #输出                                         #luhan like*football
Find index is  indexed by element:
s= ' Luhanliake ' Print (S.find (' a '))             #3print (S.find (' A ', 1))            #3print (S.index (' A ', 5))           #7
Count finds the number of occurrences of an element that can be sliced:
s= ' Luhanliake ' Print (S.count (' a '))        #2print (S.count (' R '))        #0
Replace replacement:
s = ' will happen much more Edwin van der Sar reply ' S1 = S.replace (' Edwin van der Sar ', ' little Matte ') print (s1) s2 = S.replace (' Edwin van der Sar ', ' Little Pink ', 1) print (s2) s3 = S.replace (' Edwin van der Sar ', ' SB ') print (S3)
Split split  str---> List
s = ' Alex Wusir taibai ' Print (S.split ())           #输出 [' Alex ', ' Wusir ', ' taibai ']s1 = ' alex,wusir,taibai ' Print (S1.split (', '))        #输出 [' Alex ', ' Wusir ', ' taibai ']s2 = ' alexawusirataibai ' Print (S2.split (' a '))        #输出 [', ' Lex ', ' wusir ', ' t ', ' IB ', ' I ']s3 = ' alexawusirataibai ' Print (S3.split (' A ', 1))     #分割次数, Output [', ' Lexawusirataibai ']
Format formatted output
The first kind s = ' I call {}, this year {}, hobby {} '. Format (' MT ', 18, ' cow ') print (s)
The second kind: s = ' My name {0}, this year {1}, hobby {2}, I still call {0}, this year or {1} '. Format (' MT ', 18, ' cow ') print (s)
The Third Kind: s = ' My name {name}, this year {age}, hobby {hobby} '. Format (age=18, name= ' MT ', hobby= ' stuffy son ') print (s)

Whether the strings are numbers or letters:

Name= ' 1234AA ' Print (Name.isalnum ())    #字符串由字母或数字组成print (Name.isalpha ())   #字符串只由字母组成print (Name.isdigit ())  #字符串只由数字组成

Python Basic 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.