python字符串,集合,列表,元组,字典

一.字符串

字符串是Pyhton中最常见的数据类型。我们可以使用引号(‘或“)来创建字符串。事实上,在pyhton中,加了引号的字符都被认为是字符串

python中单个字符和多个字符用引号包裹后,统称为字符串,没有java或者C中字符的说法

name = "骚姐姐"
age = "18"
age_1 = 18
msg = '''I'm Tang you''' #单引号会就近重合,使用三个单引号会避免 三单引号三双引号可以换行使用
msg = """I'm Tang you"""
print(type(name),type(age),type(age_1),type(msg))

<class 'str'> <class 'str'> <class 'int'> <class 'str'>

字符串运算及操作

数字可以进行加减乘除等运算,字符串呢?让我告诉你,也能?what?是的,但只能进行"相加"和"相乘"运算

1拼接
>>>a="hello"
>>>b="pyhton"
>>>a+b
'helloPython'

注意,字符串的拼接只能是双方都是字符串,不能跟数字或其他类型拼接

>>>age_1=5000
>>>name+age_1
Traceback(most recent call last):
  File"<stdin>",line 1,in<module>
TypeError: must be str,not 
2.重复(*)
a="hello"
print(a*5)
3.切片和索引
>>> a="Life is short,I use python"
>>> len(a)
26
>>>
>>> a[0]#索引
'L'
>>> a[16]
'u'
>>> a[-4]
't'
......
>>> a[:15]#切片
'Life is short,I'
>>> a[15:2]
''
>>> a[15::2]
' s yhn'
>>> a[::-1]
'nohtyp esu I,trohs si efiL'
>>>
4.大小写转换
 str.lower():转小写
 str.upper():转大写
 str.swapcase():大小写对换
 str.capltalize():字符串首为大写,其余小写
 str.title():以分隔符为标记,首字符为大写,其余为小写
 举例:>>>a="life is short,I use python"
>>>a.lower()#将所有大写字符转换为小写字符
'life is short,i use python'
>>>a.upper()#将所有小写字符转换为大写字符
'LIFE IS SHORT,I USE PYTHON'
>>>a.swapcase()#将所有小写字符变成打成,将大写字符变成小写
'lIFE IS SHORT, i USE PYTHON'
>>>a.capltalize()#将字符串的第一个字符大写
'Life is short, i use python'
>>>a.title()#返回标题化的字符串
'Life Is Short, I Use Python'
      
5.字符串格式输出对齐
str.center()居中
 a="Life is my love"
a.center(35,"*")
'**********Life is my love**********' #返回一个原字符串居中,并使用空格填充至长度width的新字符串
str.ljust()左对齐
>>> a.ljust(35,"*")
'Life is my love********************'
#返回一个原字符左对齐,并使用空格填充至长度width的新字符串
 str.rjust()右对齐
    >>> a="Life is short,I use python"
>>> a.rjust(35,"*")
'*********Life is short,I use python'#返回一个原字符串右对齐,并使用空格填充至长度width的新字符串
 str.zfill()
    >>>a.zfill(35)#返回长度为width的字符串,原字符串string右对齐,前面填充0,只有一个参数
'000000000Life is short,I use python'
6.删除指定字符
str.lstrip()删除左边空格
 a="****life isshort, I use python****" #删除左边指定字符串
>>> a.lstrip("*")
str.rstrip()删除右边空格
'life isshort, I use python****' #删除右边指定字符串
>>> a.rstrip("*")
str.stri()删除所有空格
>>> a.strip("*")
'life isshort, I use python'
7.计数

EXCEL表中:

 计数=countif(B2:B31,">=30")/count(B2:B31)
8.字符串搜索定位与替换
str.find()
>>> a="Life is short,I use python"
>>> a.find("e")#查找元素并返回第一个出现的索引值
3
>>> a.find("e",18,24)
18
>>> a.find("w")
-1
>>>
str.index()和find()方法一样,只不过如果str不在string中会报一个异常。
>> a="Life is short,I use python"
>>> a.index("e")
3
>>> a.index("e",18)
18
>>>a.index("w")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
>>>
str.rindex()和index方法一样,但是从右边开始
str.replace()>>> a="Life is short,I use python"
>>> a.replace("I use","You need")
'Life is short,You need python'
>>> a.replace("t","T")
'Life is shorT,I use pyThon'
>>> a.replace("t","T",1)
'Life is shorT,I use python'
9.字符串条件判断
isalnum(),字符串由字母或数字组成,
isalpha(),字符串只由字母组成,
isdigit(),字符串只由数字组成
In [1]: a = "abc123"

In [2]: b = "ABC"

In [3]: c = 123

In [4]: a.isalnum()
Out[4]: True

In [5]: a.isalpha()
Out[5]: False

In [6]: a.isdigit()
Out[6]: False

In [7]: b.isalnum()
Out[7]: True

In [8]: b.isalpha()
Out[8]: True

In [9]: b.isdigit()
Out[9]: False>>> str = '01234'
 
>>> str.isalnum()   # 是否全是字母和数字,并至少有一个字符
True
>>> str.isdigit()    # 是否全是数字,并至少有一个字符
True      
 
 
>>> str = 'string'
 
>>> str.isalnum()  # 是否全是字母和数字,并至少有一个字符
True
>>> str.isalpha()  # 是否全是字母,并至少有一个字符 
True
>>> str.islower() # 是否全是小写,当全是小写和数字一起时候,也判断为True
True
 
>>> str = "01234abcd"
 
>>> str.islower() # 是否全是小写,当全是小写和数字一起时候,也判断为True
True
 
>>> str.isalnum()  # 是否全是字母和数字,并至少有一个字符
True
 
>>> str = ' '
>>> str.isspace()  # 是否全是空白字符,并至少有一个字符
True
 
>>> str = 'ABC'
 
>>> str.isupper()   # 是否全是大写,当全是大写和数字一起时候,也判断为True
True
 
>>> str = 'Aaa Bbb'
 
>>> str.istitle()  # 所有单词字首都是大写,标题 
True
 
 
>>> str = 'string learn'
 
>>> str.startswith('str') # 判断字符串以'str'开头
True
 
>>> str.endswith('arn')  # 判读字符串以'arn'结尾
True
10.制表符转化
str.expandtabs()
>>> a = "L\tife is short, I use python"
>>> a.expandtabs() # 默认将制表符转化为8个空格
'L       ife is short, I use python'
>>> a.expandtabs(4) # 加上参数,将制表符转化为对应个数的空格
'L   ife is short, I use pythons
>>>
  1. 011ASCII码和字符的转化
chr():用一个范围在range(256)内的(就是0~255)整数作参数,返回一个对应的字符。
ord():将一个ASCII字符转换为对应的数字
chr(65)
\#结果:'A'
ord('a')
\#结果:97 

12字符串分割变换
join()将指定字符插入到元素之间;
#join
>>> str="learn string"
>>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值