Python study notes 3

xiaoxiao2021-03-06  53

1 string formatting operation

Format% variable #Format is the style of format, Variable is a variable to be formatted]

Format has the following forms

% O

# Convert numeric values ​​to octal

% x

# Convert numeric to hex

% D # integer conversion symbol

% s

# String conversion symbol

as follows:

>>> '% o'% 100

'144'

>>> '% # o'% 100 # Add a "#" number to output standard 8-based number or 16-based number

'0144'

# Single precision conversion

>>> '% .2f'% 3.235412 # .2 indicates that the two decisions are retained.

'3.24'

>>> PI = 3.1415926

>>> '% .2f'% pi # .2f does not include decumeration

'3.14'

>>> '% .2g'% pi # .2G is included in a small number

'3.1'

Original character operator "R" or "r"

>>> Print 'Hell', '/ NWROLD'

Hell

Wrold

>>> Print 'Hello', R '/ NWrOLD' #Print 'Hello', R '/ NWROLD'

Hello / NWROLD

>>>

2 Several built-in functions of the string

1 CMP (M, N) compares the size of M and N, and the m is returned to 1, and the M is returned to -1, and it will return to 0.

>>> a = 'a'

>>> b = 'b'

>>> PRINT CMP (A, B)

-1

2 look for maximum characters in strings and minimum characters max (), min ()

>>> Max ('Abcdef')

'f'

>>> MIN ('Abcdef')

'a'

3 string module

1 Find (strs, strd, m, n) lookup string STRD start position in STRS, m is the location where the lookup is started, n is the termination position

>>> Import String

>>> a = 'www.slssoft.com'

>>> B = 'SLS'

>>> Print String.Find (a, b) #default is found from the beginning

4

>>> Print String.Find (A, B, 2) # From the second place

4

>>> Print String.Find (A, B, 2, 5) # 第 位 5 位 5

-1

2 string.uppercase # 包涵 all uppercase letters

String.Lowercase # inclusted all lowercase letters

String.digits # incubated all numbers

For example, look up a string is all uppercase letters

DEF Isupper (STR):

IMPORT STRING

Temp = ''

For Temp in Str:

if string.find (String.Uppercase, Temp) == -1:

Return False

Return True

There is also a method:

>>> Print 'a' <= 'c' <= 'Z'true

转载请注明原文地址:https://www.9cbs.com/read-117555.html

New Post(0)