基础·字符串

Python基础教程

定义

4种定义方式:
(1)a = “hello” (2) a =’hello’ (3) a = “””hello””” (4) a = str()
有什么样的区别呢?
单引号和双引号没有区别,但是三引号的字符串可以换行,如下:
a=”””
im chinese
my name is David
“””

常规操作

长度

>>> a='aabbcc'
>>> len(a)
6

连接

>>> str1='abc'
>>> str2='def'
>>> str3=str1+str2
>>> print(str3)
abcdef

字符串和数字和连接

#直接连接,会报类型错误,如下
>>> i=2
>>> q='liu'
>>> t = i+q
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

这时我们就需要用到了str()函数

>>> i=2
>>> q='liu'
>>> t=str(i)+q
>>> print t
2liu

去除空格

>>> str1.strip()#去除首尾空格
'abc'
>>> str1=' abc '
>>> str1=' abc '
>>> str1.lstrip()#去左空格
'abc '
>>> str1=' abc '
>>> str1.rstrip()#去右空格
' abc'
>>> 

替换

>>> str='aaabbbccc'
>>> str.replace('b','d')#替换字符串中的b为d
'aaadddccc'
>>> str.replace('c','e',1)#替换c为e,只替换一个
'aaabbbecc'

查找字符串(find、index、rfind、rindex)

ret = a.find("a")  # 找到返回开始的索引值 0
ret = a.find("q")  # 找不到返回-1
ret = a.index("a") # 找到返回开始的索引值 0
ret = a.index("q") # 找不到会报一个异常
# 类似于 find()函数,不过是从右边开始查找.
ret = a.rfind("d") # 返回3
# 类似于 index(),不过是从右边开始.
ret = a.rindex("dd") # 找不到,报异常

查找字符串出现的次数(count)

a = "abcdeffFFFF" # 定义字符串
ret = a.count("f") # 返回2
ret = a.count("g") # 返回0

分割字符串(split,partition, rpartition, spilitlines)

a = "abcdeffFFFF" # 定义字符串
# 以"b"为分割符进行分割
ret = a.split("b") # 返回结果 ['a', 'cdeffFFFF']
# 把字符串以"cd"分割成三部分
ret= a.partition("cd") # 返回 ('ab', 'cd', 'effFFFF')
# rpartition类似于 partition()函数,从右边开始
ret = a.rpartition("f") # 返回('abcdef', 'f', 'FFFF')
# splitlines按照行分隔,返回一个包含各行作为元素的列表
 b = "hellonworld"
 c = b.splitlines()  # 返回['hello', 'world']

转化字符串大小写(capitalize,title,lower,upper)

a = "abcdeffFFFF" # 定义字符串

# 把字符串的第一个字符大写, 其余为小写
ret = a.capitalize() # 返回结果Abcdeffffff
# 把字符串的每个单词首字母大写, 其余为小写
ret = a.title() # 返回结果Abcdeffffff
# 把字符串中所有字符转化为小写
ret = a.lower() # 返回结果abcdeffffff
# 把字符串中所有字符转化为小写
ret = a.upper() # 返回结果ABCDEFFFFFF

检查字符串开头和结尾(startswith,endswith)


# 检查字符串是否是以"a"开头, 是则返回 True,否则返回 False
ret = a.startswith("a") # 返回 True
# 检查字符串是否以"f"结束,如果是返回True,否则返回 False.
ret = a.endswith("f") # 返回 False

字符串对齐方式(ljust,rjust,center)

# 返回一个原字符串左对齐,并使用字符串"x"填充至长度15的新字符串
ret = a.ljust(15, "x") # 返回结果abcdeffFFFFxxxx
# 返回一个原字符串右对齐,并使用字符串"x"填充至长度15的新字符串
ret = a.rjust(15, "x") # 返回结果xxxxabcdeffFFFF
# 返回一个原字符串居中,并使用字符串"x"填充至长度15的新字符串
ret14 = a.center(15, "x") #返回结果xxabcdeffFFFFxx

判断字符串中的字符的类型(isalpha,isdigit,isalnum, isspace)

# isalpha如果字符串中所有字符都是字母 则返回 True,否则返回False
ret = a.isalpha() # 返回True
# isdigit如果字符串中只包含数字则返回 True 否则返回 False
ret = a.isdigit() # 返回False
# isalnum如果字符串中所有字符都是字母或数字则返回 True,否则返回 False
ret25 = a.isalnum() # 返回False
# isspace如果字符串中只包含空格,则返回 True,否则返回 False.
ret26 = a.isspace() # 返回False

插入字符串,构成新字符串(join)

my_list = ["a", "b", "c"]
str = "1"
str1 = str.join(my_list) # 返回a1b1c

暂无评论

相关推荐

Python语言基础教程

一,基础  ·关键字  ·变量定义  ·字符串  ·(1)数据集合-列表&元组  (2)数据集合-字典  ·常量的定义和使用  · …

json数据操作

Python基础教程 python中操作json的api也非常的方便,我们来看一下。 我们需要用到一个库 import json 然后我们来看一 …

一些使用技巧

直接把数据库查询的查询结果中的某个字段转为集合 daily_cursor = DB_CONN.daily.find( {‘code’: ‘xxxx’, ‘date’: {‘$g …

线程

Python基础教程 在python中有两种线程的操作方式:函数或者用类来包装线程对象 用函数 import _thread import time def …

微信扫一扫,分享到朋友圈

基础·字符串