Python自动化运维开发----基础(八)字符串-创新互联

1.字符串(字符串也是列表的一种)

元江县网站建设公司创新互联建站,元江县网站设计制作,有大型网站制作公司丰富经验。已为元江县1000多家提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的元江县做网站的公司定做!

定义:单引号,双引号,三个单引号或者三个双引号引起来的

字符串的访问方式:根据索引编号访问字符串:

字符串也是列表的一种

定义:单引号,双引号,三个单引号或者三个双引号引起来的

2.字符串的访问方式

(1)根据索引编号访问

>>> name = "i am is KK"
>>> name[0]
'i'
>>> name[1]
' '
>>> name[2]
'a'

(2)遍历访问

>>> for i in name:
...     print(i)
... 
i
 
a
m
 
i
s
 
K
K

3.字符串函数


(1)len函数  统计字符串函数的长度

>>> len(name)
10

(2)max函数  字符串中大的元素


>>> max(name)
's'

(3)min函数 字符串中最小的元素

>>> min(name)
' '

(4)cout函数 查询子字符串的数量

>>> name
'i am is KK'
>>> name.count('i')
2
>>> name.count(' ')
3

(5)index函数  获取元素的索引


>>> name
'i am is KK'
>>> name.index('i')
0
>>> name.index('a')
2

(6)find函数  查找元素的位置,不存在返回-1

>>> name.find('s')
6
>>> name.find('z')
-1

(7)查找字符串中的第二个空格

>>> name
'i am is KK'
>>> name.index(' ',name.index(' ')+1)
4

(8)startswith函数 以什么开头

>>> name.startswith('i')
True
>>> name.startswith('a')
False

(9)endswith函数 以什么结尾

>>> name.endswith('K')
True
>>> name.endswith('a')
False

(10)isalnum函数  字母或者数字

>>> 'a'.isalnum()
True
>>> '@'.isalnum()
False
>>> '1'.isalnum()
True

(11)isalpha函数 判断是不是字母


>>> 'i'.isalpha()
True
>>> '1'.isalpha()
False

(12)isdecimal函数 判断是不是数字


>>> '1'.isdecimal()
True
>>> 'a'.isdecimal()
False

(13)islower函数 判断是不是小写


>>> 'a'.islower()
True
>>> 'A'.islower()
False

(14)isupper函数  判断是不是大写


>>> 'a'.isupper()
False
>>> 'A'.isupper()
True

(15)join 函数 用子字符串把list连接起来

>>> a = ['a','b']
>>> ':'.join(a)
'a:b'

(16)split函数

>>> 'a:b:c'.split(':')
['a', 'b', 'c']

(17)upper函数 转换成大写

>>> 'a'.upper()
'A'

(18)lower函数 转换成小写

>>> 'A'.lower()
'a'

(19)replace函数 替换

>>> 'abc abc'.replace('abc','x')
'x x'

(20)strip函数 取出字符串前后的空字符

>>> 'a n\f\n'.strip()
'a n'

去除指定的字符

>>> 'a b c'.strip('a')
' b c'
>>> 'a b c'.strip('a c')
'b'

(21)format函数

tpl模板

>>> tpl = 'my name is {0},and i\'m {1} years old!'
>>> name = 'likuan'
>>> age = 24
>>> tpl.format(name,age)
"my name is likuan,and i'm 24 years old!"

使用format函数传递参数

>>> '{name}-{age}'.format(name='likuan',age=24)
'likuan-24'

4.判断字符是否在字符串中


>>> 'a' in a
True
>>> 'a' not in a
False

字符串的特性 (字符串也是不可变的,不能修改和删除)

>>> a = "ab"
>>> a[0] = 'a'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

网站题目:Python自动化运维开发----基础(八)字符串-创新互联
URL分享:https://www.cdcxhl.com/article46/dssoeg.html

成都网站建设公司_创新互联,为您提供商城网站网站收录网站导航移动网站建设电子商务虚拟主机

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

手机网站建设