python基础笔记2-字符串处理函数

来源:互联网 发布:淘宝four loko是真的嘛 编辑:程序博客网 时间:2024/05/19 22:48
  1. '', "", ''' ''', """ """ 都可以定义字符串,省略使用转义字符 '\'
>>> str1='hello'>>> print str1hello>>> type(str1)<type 'str'>>>> str2='j'>>> print str2j>>> type(str2)<type 'str'>>>> str3="word">>> print str3word>>> type(str3)<type 'str'>>>> str4='''hello world'''>>> print str4hello world>>> type(str4)<type 'str'>>>> str5="""hell""">>> print str5hell>>> type(str5)<type 'str'>>>> print str3xiao>>> print str2j>>> print str2+str3jxiao>>> print str2*10jjjjjjjjjj>>> 'j' in str2True>>> 'j' in str3False>>> 'j' not in str3True>>> print 'hello\n\n\n'hello>>> print r'hello\n\n\n'  #r 使字符原始输出hello\n\n\n>>> 

python字符串格式化

>>> print 'my name is %s and weightis %d' % ('cpp', 2)   # %my name is cpp and weightis 2  
>>> mystr='hello world itcast and itcastcpp'>>> mystr.find('itcast')12>>> mystr[12:]'itcast and itcastcpp'>>> mystr.find('itcast',15) #15表示find的起始位置23>>> mystr[23:]'itcastcpp'>>> help(mystr.find) #帮助help函数查看find函数使用方法>>>> mystr.index('xxx') #找不到会抛出异常Traceback (most recent call last):  File "<stdin>", line 1, in <module>ValueError: substring not found>>> mystr.find('xxx') #找不到会显示-1-1>>> mystr.decode(encoding='UTF-8') #解码u'hello world itcast and itcastcpp'>>> s2=mystr.decode(encoding='UTF-8')>>> type(s2)<type 'unicode'>>>> type(mystr)<type 'str'>>>> mystr.replace('itcast','jing') #字符串替换'hello world jing and jingcpp'>>> mystr = mystr.replace('itcast','xx',1)  #替换多少个>>> mystr  # S.replace(old, new[, count]) -> string'hello world xx and itcastcpp'>>> >>> help(mystr.split)>>> mystr.split(' ')['hello', 'world', 'itcast', 'and', 'itcastcpp']>>> arr = mystr.split(' ')>>> for key in arr:... print key  File "<stdin>", line 2    print key        ^IndentationError: expected an indented block>>> for key in arr:...     print key... helloworlditcastanditcastcpp>>> arr = mystr.split(' ',2) #分割的列表的最后元素的下标>>> print arr['hello', 'world', 'itcast and itcastcpp']>>> mystr.capitalize() #首字母大写'Hello world itcast and itcastcpp'>>> mystr.center(50/width)  #字符串居中,且距离两边的长度width'         hello world itcast and itcastcpp         '>>> mystr.center(80)'                        hello world itcast and itcastcpp                        '>>> mystr.endswith('itcast') #以字符串单词结尾False>>> mystr.endswith('itcastcpp')True>>> mystr.startswith('hello') #以字符串单词开始True>>> >>> mystr.isalnum()  #所有字符是数字或者字符为true否则为falseFalse>>> mystr   #含有空格'hello world itcast and itcastcpp'>>> s='hello'>>> s.isalnum()True>>> s='231231231'>>> s.isalnum()True>>> s='231231231 '   #含有空格,所以false>>> s.isalnum()False>>> s='healdf '>>> s.isalpha()  #至少都是字符并且其他都是字母(不包含数字或者空格)则返回true 否则返回falseFalse>>> s='healdf'>>> s.isalpha()True>>> s='healdf23123'>>> s.isalpha()False>>> s.isdigit() #只包含数字返回true否则返回falseFalse >>> s='213123'>>> s.isdigit()True>>> s.islower()False>>> s='fsdfsdf'>>> s.islower()True>>> s='fsdfsdF'>>> s.islower()  #全部是小写False>>> s.isupper()  #全部是大写False>>> s='FSADFAS'>>> s.isupper()True>>> s='   ' >>> s.isspace()  #只包含空格True>>> s='   1' >>> s.isspace()False>>> s='  healfk'>>> s.lstrip()   #去除左空格'healfk'>>> s='  healfk   '>>> s.lstrip()  'healfk   '>>> s.rstrip()   #去除右空格'  healfk'>>> s.strip()    #去除左右空格'healfk'>>> mystr.partition('it'/str)  #把mystr以str分割为三部分('hello world ', 'it', 'cast and itcastcpp')>>> mystr.partition('itcast')('hello world ', 'itcast', ' and itcastcpp')>>> mystr.partition('world')('hello ', 'world', ' itcast and itcastcpp')
原创粉丝点击