Python data type

Source: Internet
Author: User

Data type

int numeric Type:bit_length ():Calculate number of Bits
i = 2print(i.bit_length ())  value is 2       = 5print(I.bit_length ())  value is 3       Binary 00000101
BOOL Boolean value: Int----->bool as long as 0----"False not 0 is true
i = 3= bool (i)print(b)
BOOL----> INT
True   1False  = int (true)print(i)  value is 1

Ps:while True:passwhile 1: High efficiency passstr--->bool
" " ----->"0" -----> True

sif s:print (' You entered the blank, please re-enter ')Else:Pass

stringIndexes and slicesany action on a string will form a new string, which is okay with the previous one .s = ' abcdlsesrf '#索引
S1 = S[0]print= s[2]print= s[-1]print  = s[-2]print(S4) R
 slices:

Gu Tou In spite of the end, the end needs to add 1

' ABCDLSESRF '  = s[0:4]print(S5) #ABCD
= S[0:-1]print(S6) #ABCDLSESR
= s[:] #S7 and S8 effect, do not write anything after take s8 = s[0:]print(S7,S8) # ABCDLSESRF ABCDLSESRF
= s[0:0] #空
Step:
' ABCDLSESRF '  # s[First: Tail: Step]s10 = s[0:5:2]    # two x take once print= s[4:0:-1] Print (S11)        = S[3::-1]print= s[3::-2]print(S13) #DB
take it backwards.
' ABCDLSESRF '  = s[-5:-1]      #表示从-5 to 1 of the interception display print= s[-1::-1]     #表示以-1 begins, until the beginning of the end, Display print= s[::-1] #表示全部截取 in 1 format,       shown in 1-1 print per display  (S15) #FRSESLDCBA
The operation of the string case series casing has no effect on the number capuitalize ()

# The first letter of the string is capitalized

' Arthur '  =print(S1)
#Arthur
Title () #每个隔开 (non-letter) capitalize the first letter of the word
' my +na7me,is*authur ' Print (S.title ()) #My +na7me,is*authur
Upper ()

All caps

' My name is Authur 7 days '     Print (S.upper ())
#MY NAME is AUTHUR 7 days #字母和中文是不受影响的
Lower () all lowercase
' MY NAME is AUTHUR 7 days ' Print   is Authur 7 Sunday
Examples of Use

When entering a verification code, no case

' acEQ1 '  =input (")if s_str.upper () = = You_input.upper ():     Print(' input successful ')else:     Print (' please re-enter ')
Swapcase ()

Case Flip

' MY NAme is AUTHUR ' Print (S.swapcase ())
#My NaME is Authur
Fill Center () center, default padding space, to specify fill symbol
s = ' Authur '
Print (S.center, ' + ')) #~ is the fill bit, does not fill the default is a space, 20 bits or so to add up to fill 20 bits
#+++++++authur+++++++
Expandtabs ()add \ t, if there is \ t in the string, the character before the string is less than eight bits, fill eight bits, if the preceding string satisfies eight bits, and then the eight bits
s = ' Authur\tsir '
Print (S.expandtabs ()) #结果为authur sir #所以意味着 \ t complements three bits

s = ' Authuris\tsir '
Print (S.expandtabs ()) #结果为authuris sir #所以意味着 \ t is eight-bit, because the preceding string already satisfies the eight-bit
Find Judgment Class Len () test string length, public method, string can be used, list and Ganso can also be used
' Authur ' Print (Len (s)) # 6
StartsWith () Starting with what, can be sliced
' Authur ' Print  (S.startswith ('aut', 0,5))   #默认开始和结尾不写是判断全部
#True
EndsWith () At what end, can be sliced
' Authur ' Print (S.endswith ('t', 0,3))          # By default, nothing is judged all, judging whether the tail in the selected range is the end of the specified string
#True
Find () index is found by element, can not find return-1, case-sensitive, search for a word by the index position of the first word return number, slice, default all find
' Authur ' Print (S.find ('t', 0,4))
#2
Index () indexed by element, and find using the same method, but cannot find an error delete the specified symbol strip the default before and after the deletion of space, you can add shims, such as special symbols or letters, or you can add multiple
' *   7authur  'print(S.strip ('* 7#'))
#authur
Examples of Use
Username = input (' Please enter name:'). Strip (). Lower ()if username = =' Authur ' :     ="'    ****************************    **welcome {} ' s cnblogs**    **********************  ". Format ('authur'    )print( Info
Rstrip default to remove the right space, using the same method as strip Lstrip default to remove the left space, using the same method as stripcount ()counts the number of occurrences of a specified character, which can be sliced
' Authur ' Print (S.count ('u', 0, 3))
#1
TransformationSplit ()string conversion to listsplits a string into a list by default, does not enter the content as a space, the selected character or symbol to split, the selected will disappear
' Arthur:this:my:name '  = s.split (':')print(l)
#['Arthur' This 'my 'name']
Formatted output format ()
format three ways to play formatted output s='my Name {}, this year {}, hobby {}, say again my name {}'. Format ('Authur', 18,' Python',' Authur')Print(s) name= Input ('Please enter your first name:') s='My name is {0}, this year {1}, hobby {2}, say my name {0}'. Format (name,18,' Python')Print(s) name= Input ('Please enter your first name:') s='my name {name}, this year {age}, hobby {hobby}, say again my name {name}'. Format (age=18,name=name,hobby=' Python')Print(s)
Replacereplace ()
' My name is Xiyangyang and Xiyangyang ' Print (S.replace ('xiyangyang','authur ')
#my name is Authur and Xiyangyang
is seriesThe return value is true and False
# strings consist of letters or numbers Print (Name.isalnum ()) # strings consist only of letters Print (Name.isalpha ()) # strings consist only of numbers Print (Name.isdigit ())

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