1.字符串

双引号或者单引号中的数据,就是字符串,input获取的数据,都以字符串的方式进行保存,即使输入的是数字,那么也是以字符串方式保存。

userName = input('请输入你的名字:')
print("名字:%s"%userName)
password = input('请输入你的密码:')
print("密码:%s"%password)

列表与元组下标索引,字符串实际上是字符的数组,支持下标索引。Python是以下标从0开始。切片是指对操作的对象取其中一部分的操作,字符串,列表,元组都支持切片操作。切片的语法:[起始:结束:步长],选取的区间属于左闭右开型,即从“起始”位开始,到“结束”位的前一位结束(不包含结束位本身)。

a = "012345"
    a[:3]#取前面三个字符,Out[7]: '012'
    a[::2]#步长为2,取数字,Out[8]: '024'
    a[5:1:2]#Out[9]: ''
    a[1:5:2]#第二个数到第四个数,步长取数字,Out[10]: '13'
    a[::-2]#步长为-2,取所有数字,Out[11]: '531'
    a[5:1:-2]#步长为-2.取第六个数字到第三个数字,Out[12]: '53'
    a[5::-1]#求翻转字符,Out[15]: '543210'

2.字符串常见操作:

(1)find
检测str是否包含mystr中,如果是返回开始的索引值,否则返回-1
mystr.find(str, start=0, end=len(mystr))
例子:

mystr = 'hello world itcast and itcastcpp' 
    mystr.find("itcast")#Out[21]: 12
    mystr.find("itcast",0,11)#Out[22]: -1

(2)index
跟find()方法一样,只不过如果str不在mystr中会报一个异常。

mystr.index(str, start=0, end=len(mystr))
    mystr.index("itcast",0,11)#ValueError: substring not found

(3)count
返回str在start和end之间,在mystr里面出现的次数

mystr.count(str, start=0, end=len(mystr))
    mystr.count("itcast")#Out[24]: 2

(4)replace
把mystr中的str1替换成str2,如果count指定,则替换不超过count次。

mystr.replace(str1, str2, mystr.count(str1))
    name="hello world ha ha"
    name.replace("ha","Ha")#Out[27]: 'hello world Ha Ha'
    name.replace("ha","Ha",1)#Out[28]: 'hello world Ha ha'

(5)Split
以str为分隔符切片mystr,如果maxsplit有指定值,则仅分隔maxsplit个子字符串

mystr.split(str=" ", 2)
    name.split(" ")#Out[29]: ['hello', 'world', 'ha', 'ha']
    name.split(" ",2)#Out[30]: ['hello', 'world', 'ha ha']

(6)Capitalize
把字符串的第一个字符大写

mystr.capitalize()

(7)Title
把字符串的每个单词首字母大写

name.title()#Out[33]: 'Hello World Ha Ha'

(8)Startswith
检查字符串是否是以obj开头,是则返回true,否则返回false

mystr.startswith(obj)
    name.startswith("hello")#Out[34]: TrueOut[34]: True

(9)Endswith
检查字符串是否是以obj结尾,是则返回true,否则返回false

mystr.endswith(obj)
    name.endswith("ha")#Out[36]: True

(10)Lower
转换mystr 中所有大写字符为小写

mystr.lower()
    name.lower()#Out[37]: 'hello world ha ha'

(11)Upper
转换mystr 中所有小写字符为大写

mystr.upper()
    name.upper()#Out[38]: 'HELLO WORLD HA HA'

(12)Ljust
返回一个原字符串左对齐,并使用空格填充至长度width的新字符串

mystr.ljust(width)
    name.ljust(20)#Out[41]: 'hello world ha ha   '

(13)Rjust
返回一个原字符串右对齐,并使用空格填充至长度width的新字符串

mystr.rjust(width)
    name.rjust(20)#Out[43]: '   hello world ha ha'

(14)Center
返回一个原字符串居中,并使用空格填充至长度width的新字符串

mystr.center(width)
    name.center(20)#Out[44]: ' hello world ha ha  '

(15)Istrip
删除字符串左边的空白字符

mystr.lstrip()
    names.lstrip()#Out[47]: 'hello world ha ha  '

(16)Rstrip
删除字符串右边的空白字符

mystr.rstrip()
    names.rstrip()#Out[48]: ' hello world ha ha'

(17)Strip
删除字符串两端的空白字符

names.strip()#'hello world ha ha'
    a = "\n\t itcast \t\n"
    a.strip()#Out[50]: 'itcast'

(18)Rfind
类似于find()函数,从右边开始查找

mystr.rfind(str, start=0,end=len(mystr) )
    name.rfind("ha")#Out[51]: 15

(19)Rindex
类似于index()函数,不过是从右边开始查找

mystr.rindex( str, start=0,end=len(mystr))
    name.rindex("ha")#Out[52]: 15

(20)Partition
把字符串以str分隔成三部分,str前,str和str后

mystr.partition(str)
    name.partition("ha")#Out[53]: ('hello world ', 'ha', ' ha')

(21)Rpartition
类似于Partition,只是从左边开始

mystr.rpartition(str)
    name.rpartition("ha")#Out[54]: ('hello world ha ', 'ha', '')

(22)Splitlines
按照行分隔,返回一个包含各行作为元素的列表

mystr.splitlines()
    a="ha\nha"
    print(a)
    a.splitlines()#Out[55]: ['ha', 'ha']

(23)Isalpha
检查所有字符串是否是字母,是则返回true,否则返回false

mystr.isalpha()
    a="1a2b3d"
    b="asd"
    b.isalpha()#Out[61]: True
    a.isalpha()#Out[62]: False

(24)Isdigit
检查所有字符串是否是数字,是则返回true,否则返回false

mystr.isdigit()
    a="1a2b3d"
    b="asd"
    c="123"
    a.isdigit()#Out[63]: False
    c.isdigit()#Out[66]: True

(25)Isalnum
检查所有字符串是否是字母或者字母,是则返回true,否则返回false

mystr.isalnum()
    a="1a2b3d"
    b="asd"
    c="123"
    b="12 as"
    a.isalnum()#Out[67]: True
    b.isalnum()#Out[68]: True	
    c.isalnum()#Out[69]: True
    b.isalnum()#Out[75]: False

(26)Isspace
检查所有字符串是否只包含空格,是则返回true,否则返回false

mystr.isspace()
    e=" "
    e.isspace()#Out[81]: True

(27)Join
Mystr中每个字符后面插入str,构造出一个新的字符串

mystr.join(str)
    f='hello', 'world', 'ha', 'ha'
    e=" "
    j="_"
    e.join(f)#Out[83]: 'hello world ha ha'
    j.join(f)#Out[84]: 'hello_world_ha_ha'