引号的作用:

python可以用单引号或者双引号表示字符串,用引号来区分数字和字符串

"1024"和1024和"1'02'4":
1,加引号的1024是字符串str,可使用str相关的操作;不加引号的1024表示整数或浮点数,可进行整数或浮点数的运算。
2,int(str)会把字符串强制转换成整数(如果str中有字母或者其它字符,会报错),str(int)会强制转换成字符串
3,单引号和双引号没有区别,都是表示字符串,如果在字符串中表示引号,可以用不同的引号区别,也可以用转义\'

三引号表示多行字符串,python中用\多行表示一行字符串:

"Welcome! This is the documentation for Python 3.5.0, \

last updated Sep 13, 2015. "

 

'''

Welcome! This is the documentation for Python 3.5.0, 

last updated Sep 13, 2015. 

'''

python数据类型

python有两种数据类型:字符串和非字符串

非字符串:整数int,浮点数float,复数complex-----在python2.x有长整型long

浮点数的科学计数法

13000000可表示1.3*10^7---1.3E7,0.00000013可表示1.3*10^-7----1.3E-7

 

字符串又有两种类型:

字符串str和字节bytes

字节

bytes:将普通的字符串转换成字节,如果遇到汉字需要编码成字节

>>> bytes("hello world,你好",encoding="utf-8")
b'hello world,\xe4\xbd\xa0\xe5\xa5\xbd'

 >>> "hello world,你好".encode()
 b'hello world,\xe4\xbd\xa0\xe5\xa5\xbd'

 

str:可以将字节解码成汉字

>>> str(b"hello world,\xe4\xbd\xa0\xe5\xa5\xbd",encoding="utf-8")
'hello world,你好'
>>> b"hello world,\xe4\xbd\xa0\xe5\xa5\xbd".decode()
'hello world,你好'

关于编码的内容在python官方文档的The python standard library -> binary data services -> Encodings and Unicode 

 

 

字符串

首先来一段字符串的基本操作

str1="my little pony"
str2="friendship is magic"
str3=str1+","+str2     #字符串普通的加法==>
str1+=","+str2        #通过这句话可得出==> +=的优先级大于+
print("str3=%s\nstr2=%s"%(str3,str1))

得出:
---my little pony,friendship is magic  #我的小马驹,友谊是魔法

字符串切割:

print(str2[0])
print(str2[5])
print(str2[2:7])
print(str2[2:])
print(str2[:7])
print(str2[:])

字符串也能像切割列表那样,python中基本遇到start:end:step这种,start要算进去,end不用算进去。顾头不顾尾

 

 

字符串的常用方法:

,合并,查找,插入,替换,取字符串长度,分割等。。。

取字符串长度

print(len(str1))

upper(),lower()

str3=str1.upper()
print(str3)
---MY LITTLE PONY,FRIENDSHIP IS MAGIC str1
=str3.lower() print(str1)
---my little pony,friendship is magic

capilalize(),swapcase(),casefold()

str3=str1.capitalize()
print(str3)        #头一个字母大写,其他字母都小写
---My little pony,friendship is magic
str1=str3.swapcase()
print(str1)       #大写改成小写,小写改成大写
---mY LITTLE PONY,FRIENDSHIP IS MAGIC
str3=str1.casefold()
print(str3)        #头一个字母小写,其他全是小写
---my little pony,friendship is magic

 

字符串的分割和合并

分割:split()     对象:字符串  参数一:分隔符  参数二:分割几次(默认全部分割)  返回类型:列表

合并:join()  对象:分割符  参数一:列表元祖等

#为了美观我想把,转换成空格
str1=str1.replace(',',' ')
#str1以空格为分隔符
li1=str1.split(" ")
print(li1)
---['my', 'little', 'pony', 'friendship', 'is', 'magic']
#恢复成之前的状态
str3=' '.join(li1)
print(str3)
---my little pony friendship is magic

分割二:partition()  对象:字符串  参数一:中间的字符串  返回类型:元祖

#以pony为分割点,分割成三段
str3=str1.partition("pony")
print(str3)
#---('my little ', 'pony', ',friendship is magic')

分割三(多行分割):splitline()  参数一:是否打印换行符  返回类型:列表

str1='''my little pony
friendship is magic
rainbow pony through time
'''
li1=str1.splitlines()
#---['my little pony', 'friendship is magic', 'rainbow pony through time']

查找字符串,返回字符串开始的下标

一:index()  对象:母字符串  参数一:子字符串  参数二:查找开始的位置  参数三:查找结束的位置

二:find()  对象:母字符串  参数一:子字符串  参数二:查找开始的位置  参数三:查找结束的位置

#index的用法和局限
print(str1.index("pony"))       #默认从第零个开始找
---10 print(str1.index("pony",11)) #从第11个开始找
---43 print(str1.index("pony",44,50)) #从第44个开始找到第50个,没有就会出错
#find的用法
print(str1.find("pony"))
10
print(str1.find("pony",11))
43
print(str1.find("pony",44,50))    #-1表示找不到,不会报错
-1

替换:replace()  对象:字符串  参数一:旧的子字符串  参数二:新的子字符串  参数三:替换几次(默认所有)

字符居中显示:center()  对象:字符串  参数一:指定字符串加符号一共多少个字符  参数二:剩余的地方填充符号 

print("kira".center(15,''))
★★★★★★kira★★★★★

 

字符编码:encode()

判断字符串:

开头:startswith()  对象:字符串  参数一:开头的字符串  参数二:开始位置  参数三:结束位置  返回值:布尔

结尾:endswith()  对象:字符串  参数一:结尾的字符串  参数二:开始位置  参数三:结束位置  返回值:布尔

 

计数子字符串出现的次数:count()  对象:字符串  参数一:子字符串  参数二:开始位置  参数三:结束位置  返回值:整数

posted on 2016-07-25 19:06  euewrqe  阅读(1325)  评论(0编辑  收藏  举报