Py碎碎学_之基本元素:数字、 字符串和变量
2009在选择Ruby, Python时,被名字吸引偏了方向,在迟来的8年后2017年学习过,
还停留在基础阶段,还是自身的原因没有更进一步,不仅仅是工作性质不相关的问题,
苟延残踹的阶段坚持再来过一遍
#Python基本元素:数字、字符串和变量 print('-'*20) print('#Python基本元素:数字、字符串和变量') a = 7 b = a type(a) print(type(a)) print(type(b)) print(type(58)) print(type(99.9)) print(type(False)) print(type(True)) print(type('abc')) print(5) print(+5) print(' 8 / 5 =:',8/5) print(' 8 // 5 =:',8//5) print('模 8 % 5 =:',8%5) print(' 8 ** 2 =:',8**2) print(' 2 // 3 =:',2**3) print(' 3 * 8 =:',3*8) print(' 9 divmod 5 =:',divmod(9,5)) print('十进制数 10 = ',10) print('二进制数 10 = ',0b10) print('八进制数 10 = ',0o10) print('十六进制数 10 = ',0x10) print(10**100) bottles = 99 base =' ' base += 'current inventory: ' base += str(bottles) print(base) # 2.3.5 使用 * 复制 start = 'Na ' * 4 + '\n' middle = 'Hey ' * 3 + '\n' end = 'Goodbye' print(start+start+middle+end) #2.3.6 使用[]提取字符 print('#2.3.6 使用[]提取字符') letters = 'abcdefghijklnmopqrstuvwxyz' letters[0] print(letters[0]) print(letters[1]) print(letters[-1]) print(letters[-2]) print(letters[-3]) #2.3.7 使用[start:end:step]分片 print('#2.3.7 使用[start:end:step]分片') letters2 = 'abcdefghijklnmopqrstuvwxyz' print(letters[:]) print(letters2[20:]) print(letters2[10:]) print(letters2[12:15]) print(letters2[-3:]) print(letters2[18:-3]) print(letters2[-6:-2]) print(letters2[::7]) print(letters2[4:19:3]) print(letters2[19::4]) print(letters2[:21:5]) print(letters2[-1::-1]) print(letters2[::-1]) print(letters2[::]) print('letters2[-50:] = ',letters2[-50:]) print('letters2[-51:-50] = ',letters2[-51:-50]) print('letters2[:69] = ',letters2[:69]) print('letters2[70:71] = ',letters2[70:71]) #2.3.8 使用 len()获得长度 print('-------------') print('#2.3.8 使用 len()获得长度') print(len(letters2)) empty = '' print('empty = ',empty) #2.3.9 使用 split()分割 string.split(arguments) print('-------------') print('#2.3.9 使用 split()分割') totos = 'get gloves, get mask, give cat vitamins, call ambulance' #print('totos.split(',') = ', totos.split(',')) print("totos.split(',') = ",totos.split(',')) print("totos.split('/') 这样没有分隔开 = ",totos.split('/')) ### 注意到用 / 没有分割开字符,还是一个字符串 print('totos.split() = ',totos.split()) #2.3.10 使用 join()合并 string.join(list) , 如 '.'.join(lines) print('-------------') print('#2.3.10 使用 join()合并') crypto_list = ['Yeti','Bigfoot','Loch Ness Monster'] crypto_string = '.'.join(crypto_list) print(" '.'.join(crypto_list) = ",crypto_string ) crypto_str_2 = '/'.join(crypto_list) print(" '/'.join(crypto_list) = ",crypto_str_2) #2.3.11 熟悉字符串 print('-------------') print('#2.3.11 熟悉字符串') poem = '''All that doth flow we cannot liquid name Or else would fire and water be the same; But that is liquid which is moist and wet Fire that property can never get. Then 'tis not cold that doth the fire put out But 'tis the wet that makes it dte, no doubt.''' print('提取开头的13个字符 = ',poem[:13]) print(len(poem)) print(poem.startswith('All')) print(poem.endswith('That\'s all,folks!')) word = 'the' print('第1次出现 the 的位置 = ',poem.find(word)) print('第1次出现 the 的偏移量 = ',poem.rfind(word)) print('出现 the 的次数 = ', poem.count(word)) print('诗中出现的所有字符都是字母或数字吗? = ',poem.isalnum()) #2.3.12 大小写对齐方式 print('-------------') print('#2.3.12 大小写对齐方式') setup = 'a duck goes into a bar...' print('原有字符串 = ',setup) print('将字符串收尾的 . 都删除掉 = ',setup.strip('.')) print('将字符串首字母变为大写 = ',setup.capitalize()) print('将所有单词开头字母变为大写 = ',setup.title()) print('将所有字母都变为大写 = ',setup.upper()) print('将所有字母都变为大写 = ',setup.lower()) print('将所有字母都变为大写 = ',setup.swapcase()) print('将给定字符串排版在给定长度30个字符空间里,居中 = ',setup.center(30)) print('将给定字符串排版在给定长度30个字符空间里,左对齐 = ',setup.ljust(30)) print('将给定字符串排版在给定长度30个字符空间里,右对齐 = ',setup.rjust(30)) #2.3.13 大小写对齐方式 print('-------------') print('#2.3.13 使用replace()替换') print(setup.replace('duck','marmoset')) print(setup.replace('a ','a famous ',100)) print('......今日头条上的一道题......') #给出一个句子,不允许用 split 函数,对遍历、找出单词中最长的单词 #这个是之前在今日头条里面看到的,说面试过很多人不会的 str_1 = '''Life is a journey, not the destination, but the scenery along the should be and the mood at the view.''' print(str_1.replace(',','').replace('.','').split(' ')) #用了2个replace替换掉2种符号,再用split分割成字符串列表 print(lambda x:len(x),str_1.replace(',','').replace('.','').split(' ')) print(max(map(lambda x:len(x),str_1.replace(',','').replace('.','').split(' ')))) str_a ='Our Country is great!' print(max(map(lambda x:len(x),str_a.replace(',','').replace('.','').split(' ')))) str_2 = 'a two three four five six seven eight nine ten' str_tmp = str_2.split(" ") maxlen = max(len(i) for i in str_tmp) print(maxlen) #只是找出最长单词的长度