1. 程式人生 > >Python基礎(數字,字串方法)

Python基礎(數字,字串方法)

數字:

1 #二進位制轉十進位制
2 a='100'
3 v=int(a,base=2)
4 print(v)
進位制轉換
1 #當前數字的二進位制至少有多少位
2 b=2
3 v2=b.bit_length()
4 print(v2)
數值二進位制表示位數

字串:

1 #首字母大寫
2 a="kelvin"
3 v=a.capitalize()
4 print(v)
首字母大寫
1 #終極無敵'大寫'變'小寫'---->casefold
2 b="KELVIN
" 3 v1=b.casefold() 4 v2=b.lower() 5 print(v1) 6 print(v2)
大寫轉小寫
1 #居中顯示字串
2 c="kelvin"
3 v3=c.center(50,"-") #第二個引數不填,預設空格
4 print(v3)
劇中顯示
1 #查詢在字串中指定序列出現次數
2 d="kelvinvinkddq"
3 num=d.count("d")
4 print(num)
查詢指定序列出現次數
 1 #判斷是否以指定字元序列開頭或者結尾
 2 e="
nihaoyahahakelvin" 3 v4=e.endswith("in") 4 v6=e.endswith("o") 5 v5=e.startswith("ni") 6 v7=e.startswith("ll") 7 print(v4) 8 print(v6) 9 print(v5) 10 print(v7)
是否以指定序列結尾或開頭
1 #查詢字串中是否有指定序列
2 f="fsdhjheujfdkelvinvfs d"
3 num1=f.find("kelvin")
4 num2=f.find("fsdf")
5 print(num1)
6 print(num2)
查詢指定序列並返回索引
1 #字串格式化
2 g="i am {name},age {age}"
3 v8=g.format(name="alex",age=19)
4 v9=g.format_map({"name":"kelvin","age":21})
5 print(v8)
6 print(v9)
字串格式化
1 #返回指定序列索引,不存在則報錯
2 a="kelvinalapplebanag"
3 v=a.index("w")
4 print(v)
返回指定序列索引不存在就報錯
1 str = "this2009";  # 字元中沒有空格
2 print (str.isalnum());
3 
4 str = "this is string example....wow!!!";
5 print(str.isalnum());
判斷字串是否只含數字和字母
1 str = "this";  # No space & digit in this string
2 print str.isalpha();
3 
4 str = "this is string example....wow!!!";
5 print str.isalpha();
如果 string 至少有一個字元並且所有字元都是字母則返回 True, 否則返回 False
1 str = "123456";  # Only digit in this string
2 print str.isdigit();
3 
4 str = "this is string example....wow!!!";
5 print str.isdigit();
如果 string 只包含數字則返回 True 否則返回 False.
1 str = "       "; 
2 print str.isspace();
3 
4 str = "This is string example....wow!!!";
5 print str.isspace();
如果 string 中只包含空格,則返回 True,否則返回 False.
1 tr = "This Is String Example...Wow!!!";
2 print str.istitle();
3 
4 str = "This is string example....wow!!!";
5 print str.istitle();
如果 string 是標題化的(見 title())則返回 True,否則返回 False
1 str = "-";
2 seq = ("a", "b", "c"); # 字串序列
3 print str.join( seq );
以 string 作為分隔符,將 seq 中所有的元素(的字串表示)合併為一個新的字串
1 str = "this is string example....wow!!!";
2 
3 print str.ljust(50, '0');
返回一個原字串左對齊,並使用空格填充至長度 width 的新字串
1 str = "     this is string example....wow!!!     ";
2 print str.lstrip();
3 str = "88888888this is string example....wow!!!8888888";
4 print str.lstrip('8');
截掉字串左邊的空格或指定字元。
 1 from string import maketrans   # 必須呼叫 maketrans 函式。
 2 
 3 intab = "aeiou"
 4 outtab = "12345"
 5 trantab = maketrans(intab, outtab)
 6 
 7 str = "this is string example....wow!!!";
 8 print str.translate(trantab);
 9 
10 以上例項輸出結果如下:
11 
12 th3s 3s str3ng 2x1mpl2....w4w!!!
建立字元對映的轉換表
1 str = "this is really a string example....wow!!!";
2 print "Max character: " + max(str);
3 
4 str = "this is a string example....wow!!!";
5 print "Max character: " + max(str);
返回字串中最大的字母。
1  
2 str = "00000003210Runoob01230000000"; 
3 print str.strip( '0' );  # 去除首尾字元 0
4  
5  
6 str2 = "   Runoob      ";   # 去除首尾空格
7 print str2.strip();
移除字串頭尾指定的字元
1 str = "this is string example....wow!!!";
2 print str.swapcase();
3  
4 str = "THIS IS STRING EXAMPLE....WOW!!!";
5 print str.swapcase();
翻轉 string 中的大小寫

以上是經常使用的內建方法。