字符串

def spliting():
return('-'*30)
print('spa"m') # spa"m
print("spa'm") # spa'm
print('''spam''') # spam
print('spam',"spam") # spam spam
print('l' 'u''x') # lux
print('spa'm',"spa"m") # spa'm spa"m

spliting()

a = 'a\0b\0c' # a
print(len(a)) # 5
a = '\001\002\003'
print(repr(a)) # '\x01\x02\x03'
print(len(a)) # 3
a = 's\tp\na\x00m'
print(repr(a),len(a)) # 's\tp\na\x00m' 7
a = 'c:\py\code'
print(len(a)) # 10
a = 'a\tbc'
print(a,len(a)) # a bc 4
a = 'a\tbc'
print(a,len(a)) # a\tbc 5
a = r'a\tbc'
print(a,len(a)) # a\tbc 5

spliting()
a = 1
import os
print(os.getcwd())
y = 2

a = 'spam'
for i in a:print(i,end = '') # spam
print('1234'[::-1]) # 4321
print('1234'[slice(None,None,-1)])
a = 'qbcdefg'
print(a[1:5:-1]) # 输出' '
print(a[5:1:-1]) # fedc
print(a[slice(5,1,-1)]) # fedc
print('spam'[2:4]) # am
print('spam'[slice(2,4)]) # am

import sys
print(sys.argv)

cmd中执行人如下;

F:\allcode\day1code\etc>shiyan.py

['F:\allcode\day1code\etc\shiyan.py']

F:\allcode\day1code\etc>

print('1+2+3') # 1+2+3
print(eval('1+2+3')) # 6
print(ord('s')) # 115
print(chr(115)) # s
a = '5'
print(chr(ord(a)+1)) # 6
B = '1101'
I = 0

while B != '':
I = I * 2 + (ord(B[0])-ord('0'))
B = B[1:]
print(I) # 13
print(int('1101',2)) # 13

print(ord('0'))
a = 'spam'
a = a.replace('sp','sb')
print(a)

spliting()

print('spam lux'.capitalize()) # Spam lux
print('spam lux'.center(10,'-')) # -spam lux-
print('spam lux'.count('m')) # 1
print('spam lux'.encode('utf-8')) # b'spam lux'
print(bytes('spam lux',encoding = 'utf-8')) # b'spam lux'
print('spam lux'.endswith('am',1,4)) # True
print('spam lux'.endswith('am')) # False
print('spam lux'.expandtabs()) # hello spam lux
print('spam lux'.find('m',1,4)) # 3
print('spam lux'.find('u',1,4)) # -1
print('hello {}'.format('spam lux')) # spam lux
print('spam lux'.index('m',1,4)) # 3

print('spam lux'.index('u',1,4)) # error

print('spam lux'.isalnum()) # False
print('1433'.isalnum()) # True
print('spam lux'.isalpha()) # False
print('spamlux'.isalpha()) # True
print(u'rtt6t'.isdecimal()) # False
print(u'12345'.isdecimal()) # True
print('234'.isdigit()) # True
print('&hgfgf'.isidentifier()) # False
print('dfhfhdfj'.isidentifier()) # 检测有效的标识符
print('spam lux'.islower()) # True
print('SPAM LUX'.isupper()) # True
print('4EHF$E'.isnumeric()) # False
print('456'.isnumeric()) # 只有数字,针对unicode
print('spam'.ljust(10,'')) # spam*****
print('spam'.rjust(10,'*')) # ******spam
print('sPAMm lux'.lower()) # spamm lux
print(' spam '.lstrip()) # 'spam '
print('spsasm'.partition('p')) # ('s', 'p', 'sasm')
print('spam'.replace('pa','dd')) # sddm
print('spam lux spam'.rfind('s')) # 9
print('spam lux spam'.rindex('s')) # 9
print('sbcsbbsba'.rpartition('s')) # ('sbcsbb', 's', 'ba')
print('s p a m'.rsplit()) # ['s', 'p', 'a', 'm']
print('s-pa-m'.split('-')) # ['s', 'pa', 'm']
print(' spam '.rstrip()) # spam
print('a\nb c'.splitlines()) # ['a', 'b c']
print('spam lux'.startswith('s')) # True
print(' spam '.strip()) # spam
print('sdf%sdf'.isprintable()) # True
print(' '.isspace()) # True
print('d f'.isspace()) # False
print('Spam Lux'.istitle()) # True
print('SPAM LUX'.isupper()) # True
print('-'.join(['1','2','3'])) # 1-2-3
print('spam LUX'.swapcase()) # SPAM lux
print('spam lux'.title()) # Spam Lux
print('spam lux'.upper()) # SPAM LUX
print('spam'.zfill(10)) # 000000spam
a =str.maketrans('spam','luxs')
print('spam lux'.translate(a)) # luxs lux

a = 1234
print('%d......%-8d......%06d' % (a,a,a)) # 1234......1234 ......001234
a = 1.23456789
print('%e | %.2f | %g' % (a,a,a)) # 1.234568e+00 | 1.23 | 1.23457
print('%-6.2f %05.2f %+06.1f' % (a,a,a)) # 1.23 01.23 +001.2
print('%f %.2f %.*f' % (1/3.0,1/3.0,4,1/3.0)) # 0.333333 0.33 0.3333
print('%(n)d %(a)s' % {'n':1,'a':'spam'}) # 1 spam

a = 'hello every ,my name is %(name)s ,I am %(age)d years old'
b = {'name': 'lux','age':18}
print(a % b)

hello every ,my name is lux ,I am 18 years old

food = 'spam'
age = 18
print(vars()) # 返回存在的所有变量
a = '{0}-->{1}-->{2}'
print(a.format(1,2,3)) # 1-->2-->3
a = '{b}{c}{d}'
print(a.format(b='ww',c='rr',d='yy')) # wwrryy
import sys
print('my {1[com]} is {0.platform}'.format(sys,{'com':'linux'}))

my linux is win32

print('my {com[ss]} is {s.platform} systerm'.format(s =sys,com = {'ss':'linux'}))

my linux is win32 systerm

somelist = list('spam')
print('first is {0[0]} third is {0[2]}'.format(somelist))

first is s third is a

print('first is {0[1]}last is {1}'.format(somelist,somelist[-1]))

first is plast is m 括号里不能用负索引

parts = somelist[0],somelist[-1],somelist[1:3]
print('first {0} lats {1} middle{2}'.format(*parts))

first s lats m middle['p', 'a']

x = 1234
print('%d. .%-6d. .%06d' % (x, x, x)) # 1234. .1234 . .001234

x = 1.23456789
print('%f | %e | %g' % (x, x, x)) # 1.234568 | 1.234568e+00 | 1.23457

左对齐,补零,正负号

x = 1.236458
print('%-3.2f | %06.2f | %+06.2f' % (x, x, x)) # 1.24 | 001.24 | +01.24

posted on 2019-07-08 08:20  幻嘤剑舞  阅读(656)  评论(0编辑  收藏  举报

导航