'', "", ''' ''', """ """
都可以定义字符串,省略使用转义字符'\'
>>> str1='hello'
>>> print str1
hello
>>> type(str1)
<type 'str'>
>>> str2='j'
>>> print str2
j
>>> type(str2)
<type 'str'>
>>> str3="word"
>>> print str3
word
>>> type(str3)
<type 'str'>
>>> str4='''hello world'''
>>> print str4
hello world
>>> type(str4)
<type 'str'>
>>> str5="""hell"""
>>> print str5
hell
>>> type(str5)
<type 'str'>
>>> print str3
xiao
>>> print str2
j
>>> print str2+str3
jxiao
>>> print str2*10
jjjjjjjjjj
>>> 'j' in str2
True
>>> 'j' in str3
False
>>> 'j' not in str3
True
>>> 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
...
hello
world
itcast
and
itcastcpp
>>> 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否则为false
False
>>> 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 否则返回false
False
>>> s='healdf'
>>> s.isalpha()
True
>>> s='healdf23123'
>>> s.isalpha()
False
>>> s.isdigit() #只包含数字返回true否则返回false
False
>>> 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')