跳至主要内容

博文

目前显示的是 十月, 2019的博文

python 之 List(列表)

https://www.python-course.eu/python3_list_manipulation.php 模拟超市购物 shopping_list = [ 'milk' , 'yoghurt' , 'egg' , 'butter' , 'bread' , 'bananas' ] cart = [ ] while shopping_list != [ ] :     article = shopping_list. pop ( )       cart. append ( article )     print ( article , shopping_list , cart )   print ( "shopping_list: " , shopping_list ) print ( "cart: " , cart )   letter salad s = "Toronto is the largest City in Canada" t = "Python courses in Toronto by Bodenseo" # print(list(zip(s,t))) ss = "" . join ( [ "" . join ( x ) for x in zip ( s , t ) ] ) print ( ss ) #TPoyrtohnotno  ciosu rtshees  lianr gTeosrto nCtiot yb yi nB oCdaennasdeao print ( ss [ :: 2 ] ) #Toronto is the largest City in Canada print ( ss [ 1 :: 2 ] ) #Python courses in Toronto by Bodenseo 重复的陷阱 x = [ "a" , "b" , "c" ] y = [ x ] * 4   print ( y ) # [[...

python 之 Strings (字符串)

https://www.programiz.com/python-programming/string Python不支持字符类型,它们被视为长度为1的字符串,也被视为子字符串 字符串是一系列字符。它可以通过使用双引号在python中声明。字符串是不可变的, 它表明,一旦将String绑定到变量,它能被删除,但 不能被修改 通过在引号中放置一系列字符来创建字符串。字符串可以用单引号,双引号或三引号括起来,它们由三个单引号或三个双引号组成 三引号中的字符串可以跨越多行而不使用转义字符 默认情况下,Python 2中的字符串是非Unicode(ASCII),但也支持Unicode。 另一方面,Python 3字符串都是Unicode(UTF-8) Strings in Python 2. >>> print ( type ( 'Python String' )) < type 'str' > >>> print ( type ( u 'Python Unicode String' )) < type 'unicode' > Strings in Python 3. >>> print ( type ( 'Python String' )) < class 'str' > >>> print ( type ( u 'Python Unicode String' )) < class 'str' > Python中的字符串运算符 字符串 索引 查看字母x在字符串中的索引位置 string.index('x') 字符串 切片 Slicing [ ]   Range Slicing [X:y] 使用方括号与索引或索引范围一起切​​片以获得子串 string = "python" print(string[4]) slice运算符在给定索引处打印字符 print(string[2:4]) 打印给...