Learn Python By Practice — string,pythonpractice,import sys#
文章由Byrx.net分享于2019-03-23 08:03:12
Learn Python By Practice — string,pythonpractice,import sys#
import sys# coding: UTF-8def stringTest(): # Python中的字符串是不可变的,同Java一样,所以下面创建了三个字符串对象 msg = "Hello" + " world!" print("string concat: " + msg) # 可以通过索引方式访问字符串中的单个字符 print("string access by index: " + msg[1]) # 字符串长度函数,len print("string len function: lengh of hello is " + str(len("hello"))) # 字符串不能直接与数字进行 + 运算(即不支持隐式转换数字类型到字符串类型),必须通过str函数将数字转成字符串 # print(msg + 3.14) # wrong print("str function: " + (msg + str(3.14))) # 布尔类型也如此 # print(msg + True) # 但是可以这样进行普通字符串与数字或布尔值拼接,但是注意拼接得到的两个字符串之间会有一个空格 print(msg, 3.14) # Hello world! 3.14 print(msg, True) # Hello world! True # raw字符串,不进行转义处理 rawStr = r"this\n\r\t is a good habit" print("raw string: " + rawStr) # 跨行字符串 multiLineStr = """It's the best timeIt's the worst time""" print("multiline string: " + multiLineStr) # 字符串相关函数 # s.lower()、s.upper() print("string lower function: " + "hello".upper()) print("string upper function: " + "WORLD".lower()) # s.strip()移除首尾空格(包括\r\t...) print("string strip function: " + " hello world \t".strip()) # s.isalpha() print("a".isalpha()) # s.isdigit() print("314".isdigit()) # True print("3.14".isdigit()) # False print("314".isnumeric()) print("abc314".isalnum()) # True # 其他的还有islower() isupper() isspace() # ... # s.startswith(other) s.endswith(other) print("hello".startswith("h")) print("world".endswith("d")) # s.find(other),以other的第一个字符为准进行查找,如果找到返回这个字符在s中的索引,否则返回-1 print("string find function: o locate at index " + str("hello".find("o"))) print("o" in "hello") # s.replace(old, new) print("string replace function: " + "hello".replace("ello", "appy")) # s.split(delimeter) 获得字符串列表 print("string split function: " + str("hello world".split(" "))) # s.join(list) 将字符串列表中的元素用字符串s连起来 print("string join function: " + "###".join(["hello", "world"])) # 字符串切片slice # hello 从左到右的index为 0 1 2 3 4,而从右到左的index为-1 -2 -3 -4 -5。即0对应-5...4对应-1 print("string slice function: " + "hello"[1:3]) # 从index为1的字符开始切,切3-1=2个字符 print("string slice function: " + "hello"[1:5]) print("string slice function: " + "hello"[1:6]) print("string slice function: " + "hello"[2:]) print("string slice function: " + "hello"[:3]) print("string slice function: " + "hello"[:]) # 字符串复制 print("string slice function: " + "hello"[-1]) print("string slice function: " + "hello"[:-3]) # 不包括最后3个字符 print("string slice function: " + "hello"[-3:]) # 从倒数第三个字符开始至最后一个字符 print("string slice function: " + "hello"[0:-2]) # hel print("string slice function: " + "hello"[-1:0]) # 空字符串 print("string slice function: " + "hello"[-1:1]) # 空字符串 print("string slice function: " + "hello"[-2:2]) # 空字符串 print("string slice function: " + "hello"[-5:-1])# hell # 注意除了字符串外,其他列表类型也支持切片 # 字符串格式化 %的应用 text = "%d fox jumps over the %s" % (1, "fence") print("string format by %: " + text) # 跨行 text = ("%d pigs walk towards the %s" % (2, "wall")) print("string format by % corss line: " + text) # unicode字符串 # Python 3中所有的字符串都是Unicode字符串 # 而Python 3之前可以这样声明一个Unicode字符串 s = u"unicode string" # bytes类型对象 bso = b"bytes object+" print("bytes object: " + str(bso)) print("bytes object concat: " + str(bso + b" 2")) print("bytes object length: " + str(len(bso))) # bytes类型对象只能包含ASCII字符,不能包含中文 # bso = b"bytes object+中文" # 运行时报错“SyntaxError: bytes can only contain ASCII literal characters.” # bytearray类型对象 bao = bytearray(bso) print(str(bao)) # bytearray(b'bytes object+') # 索引方式操纵字节 bao[0] = 99 print(str(bao)) # 字符串编码解码 myStr = "Python编程" print("myStr original length: " + str(len(myStr))) utf8EncodedStr = myStr.encode("utf-8") print(utf8EncodedStr, len(utf8EncodedStr)) gb18030EncodedStr = myStr.encode("gb18030") print(gb18030EncodedStr, len(gb18030EncodedStr)) # utf8解码 utf8DecodedStr = utf8EncodedStr.decode("utf-8") print(utf8DecodedStr) # Python 3支持的编码方式见:http://docs.python.org/2/library/codecs.html#standard-encodingsdef main(): stringTest()if __name__ == '__main__': main()
评论关闭