PYTHON笔记-字符串

来源:互联网 发布:编程图标 编辑:程序博客网 时间:2024/06/05 21:56

python编码

编码 函数 unicode 编码到 utf-8 gbk encode utf-8 gbk 解码到unicode decode

字符串

name = 'chen'print(dir(name))

format

warnContent = "城市:{city}"print(warnContent.format(city = '中山'))
stdStr = 'chenlong'print(stdStr.capitalize())# C:\Python35\python.exe D:/python/hostloc/oldboy/day02/dir.py# ChenlongstdStr = 'STRONG'print(stdStr.casefold())#strongprint(stdStr.center(15,'$'))#$$$$$STRONG$$$$#15个符号中,进行居中stdStr = 'To be or not to be,It is a question'print(stdStr.count('o'))#5      计数print(stdStr.count('o',0,9))#2      标注起止位置stdStr = '个人python练习'print(stdStr.encode('gbk'))#b'\xb8\xf6\xc8\xcbpython\xc1\xb7\xcf\xb0'  即转化为gbk编码print(stdStr.endswith('习'))#True   判断结尾字符stdStr = '个人\tpython练习'print(stdStr)print(stdStr.expandtabs())# 个人    python练习# 个人      python练习#将制表符转化为8个空格print(stdStr.__len__())print(len(stdStr.expandtabs()))# 11# 16    测量字符串长度print(stdStr.find('python'))# 3     返回查找的字符串的起始位置print(stdStr.find('csharp'))# -1    查找失败则返回-1# print(stdStr.index('csharp'))#区别于find,如果index查找失败则触发异常# Traceback (most recent call last):#   File "D:/python/hostloc/oldboy/day02/dir.py", line 39, in <module>#     print(stdStr.index('csharp'))# ValueError: substring not foundstdList = ['Hi', 'My', 'Name', 'Is', 'Python']print(' '.join(stdList))# Hi My Name Is Python      字符串拼接stdStr = 'IamPython'print(stdStr.partition('am'))# ('I', 'am', 'Python')stdStr = 'I,am,Python'print(stdStr.partition(','))#('I', ',', 'am,Python')#只能分割成3部分stdStr = 'Hello Python'print(stdStr.replace('o','*'))#Hell* Pyth*n   字符串替换print(stdStr.replace('o','*',1))#Hell* Python   添加替换次数stdStr = 'I,am,Python'print(stdStr.split(','))#['I', 'am', 'Python']      字符串拆分print(stdStr.strip('I'))#,am,Python     字符串去开头结尾制定字符

字典操作

stdDict = dict(name = 'Chen',age = 18, Home = "liaoNing",weight = 175)print(stdDict.get('career','coder'))print(stdDict.pop('weight'),stdDict)print(stdDict.popitem(),stdDict)# coder# 175 {'Home': 'liaoNing', 'name': 'Chen', 'age': 18}# ('Home', 'liaoNing') {'name': 'Chen', 'age': 18}
0 0