Learning method of a digital built python 08-- basic data types and string types of type

A digital type int and float built-in method

1.int type conversion

 1.1 pure digital converted into a string of int

# res=int('100111')

# print(res,type(res))

 1.2 hex conversion

1.2.1 turn into other binary decimal

# 10 decimal -> binary

# 11 - > 1011

# 1011-> 8+2+1

# print(bin(11)) # 0b1011

 

# 10 hex -> octal

# print(oct(11)) # 0o13

 

# 10 hex -> Hex

# print(hex(11)) # 0xb

# print(hex(123)) # 0xb

 

 1.2.2 Other converted into its decimal system

# Binary -> Decimal

# print(int('0b1011',2)) # 11

 

# Binary -> octal

# print(int('0o13',8)) # 11

 

# Binary -> hexadecimal

# print(int('0xb',16)) # 11

3. Use

Digital type is mainly used for the mathematical operation and comparison operation, so in addition to the digital type used in conjunction with the operator, the need to master built-in method

 

Two built string methods

 

 1. Definitions

# msg='hello' # msg=str('msg')

# print(type(msg))

 

 2. Type Conversion

# Str can be any other type strings are transformed into

# res=str({'a':1})

# print(res,type(res))

 

 3. Use: built-in methods

 3.1 Priority grasp

 3.1.1, according to the index value (forward + reverse take take): can only take

msg='hello world'

# Take forward

# print(msg[0])

# print(msg[5])

# Reverse take

# print(msg[-1])

 

# Can only take

# msg[0]='H'

 

 3.1.2 Slice: expand the application index, a copy of a substring from a string of large

msg='hello world'

# Care regardless of the end

# res=msg[0:5] #x

# print(res)

# print(msg)

 

# Steps

# res=msg[0:5:2] # 0 2 4

# Print (res) # survey

 

# Reverse steps (understanding)

# res=msg[5:0:-1]

# print(res) #" olle"

 

msg='hello world'

# res=msg[:] # res=msg[0:11]

# print(res)

 

# Res = msg [:: - 1] # string upside down

# print(res)

 

 3.1.3 length len

# msg='hello world'

# Print (len (msg))

 

 3.1.4, members of the operations in and not in

# Determines a substring is present in a large string

# print("alex" in "alex is sb")

# print("alex" not in "alex is sb")

# Print (not "alex" in "alex is sb") # is not recommended

 

 3.1.5, left and right sides of the symbol string is removed strip

# Default remove spaces

# Msg = '       egon       '

# res=msg.strip()

# Print (msg) # do not change the original value

# Print (res) # is the creation of new value

 

# Default remove spaces

# Msg = '**** egon ****'

# print(msg.strip('*'))

 

Learn #: strip only take on both sides, not the intermediate

# msg='****e*****gon****'

# print(msg.strip('*'))

 

# Msg = '** / * = - ** egon ** - = () **'

# print(msg.strip('*/-=()'))

 

# Application

# inp_user=input('your name>>: ').strip() # inp_user=" egon"

# inp_pwd=input('your password>>: ').strip()

# if inp_user == 'egon' and inp_pwd == '123':

#     Print ( 'Login successful')

# else:

#     Print ( 'account password wrong')

 

  3.1.6, segmentation split: a string to be segmented according to some separators, give a list of

# # The default separator is a space

# Info = 'egon 18 male'

# res=info.split()

# print(res)

 

## specify the delimiter

# Info = 'egon: 18: male'

# res=info.split(':')

# print(res)

 

# Specify separate times (understand)

# Info = 'egon: 18: male'

# res=info.split(':',1)

# print(res)

 

 3.1.7, cycling

# Info = 'egon: 18: male'

# for x in info:

#     print(x)

 

 4.2 need to know (**)

#4.2.1、strip,lstrip,rstrip

# Msg = '*** egon ****'

# print(msg.strip('*'))

# print(msg.lstrip('*'))

# print(msg.rstrip('*'))

 

#4.2.2、lower,upper

# msg='AbbbCCCC'

# print(msg.lower())

# print(msg.upper())

 

#4.2.3、startswith,endswith

# print("alex is sb".startswith("alex"))

# print("alex is sb".endswith('sb'))

 

#4.2.4、format

 

# 4.2.5, split, rsplit: cut the string list

# info="egon:18:male"

# print(info.split(':',1)) # ["egon","18:male"]

# print(info.rsplit(':',1)) # ["egon:18","male"]

 

# 4.2.6, join: the list spliced ​​into a string

# L = [ 'Egon', '18', 'male']

# res=l[0]+":"+l[1]+":"+l[2]

# Res = ":". Join (l) # accordance with a delimiter, the character string list of all the elements together into a large string

# print(res)

 

# l=[1,"2",'aaa']

# ":".join(l)

 

#4.2.7、replace

# msg="you can you up no can no bb"

# print(msg.replace("you","YOU",))

# print(msg.replace("you","YOU",1))

 

#4.2.8、isdigit

# Determines whether a string of digits of pure

# print('123'.isdigit())

# print('12.3'.isdigit())

 

# Age = input ( 'Please enter your age:') strip ().

# if age.isdigit():

#     age=int(age) # int("abbab")

#     if age > 18:

#         Print ( 'big guess')

#     elif age < 18:

#         Print ( 'guess small')

#     else:

#         Print ( 'only the best')

# else:

#     Print ( 'must enter a number, fool')

 

# 4.3 Learn

#4.3.1、find,rfind,index,rindex,count

msg='hello egon hahaha'

# Find return to the starting index

# Print (msg.find ( 'e')) # Returns the start index string to search for the string in the large

# print(msg.find('egon'))

# print(msg.index('e'))

# print(msg.index('egon'))

# Can not find

# Print (msg.find ( 'xxx')) # returns -1, representing not found

# Print (msg.index ( 'xxx')) # thrown

 

# Msg = 'hello egon hahaha egon, egon'

# print(msg.count('egon'))

 

# 4.3.2, center, light, rjust, zfill

# Print ( 'egon'.center (50,' * '))

# Print ( 'egon'.ljust (50, +))

# Print ( 'egon'.rjust (50,' * '))

# Print ( 'egon'.zfill (10))

 

#4.3.3、expandtabs

# msg='hello\tworld'

Number of spaces # print (msg.expandtabs (2)) # tab represents the set of 2

 

# 4.3.4, captalize, swapcase, title

# print("hello world egon".capitalize())

# print("Hello WorLd EGon".swapcase())

# print("hello world egon".title())

 

# 4.3.5, is digital series

# 4.3.6, is the other

# print('abc'.islower())

# print('ABC'.isupper())

# print('Hello World'.istitle())

# Print ( '123123aadsf'.isalnum ()) # string of letters or numbers to True

# Print ( 'ad'.isalpha ()) # string of the alphabet is True

Print # ( '     ' .isspace ()) # string to True whitespace

# Print ( "print'.isidentifier ())

# Print ( "age_of_egon'.isidentifier ())

# Print ( "1age_of_egon'.isidentifier ())

 

 

num1=b'4' #bytes

u = num2 '. 4' # unicode, without adding to python3 u is the unicode

= num3 'four' # Chinese digital

= Num4 'IV' # Roman numeral

 

# Isdigit only identify: num1, num2

# print(num1.isdigit()) # True

# print(num2.isdigit()) # True

# print(num3.isdigit()) # False

# print(num4.isdigit()) # False

 

 

 

# Isnumberic can be identified: num2, num3, num4

# print(num2.isnumeric()) # True

# print(num3.isnumeric()) # True

# print(num4.isnumeric()) # True

 

# Isdecimal only recognizes: num2

print(num2.isdecimal()) # True

print(num3.isdecimal()) # False

print(num4.isdecimal()) # False

Guess you like

Origin www.cnblogs.com/heirenxilou/p/12455196.html