美文网首页
Python字符串的使用

Python字符串的使用

作者: 行走的浮游 | 来源:发表于2018-01-21 11:07 被阅读0次

1、编码问题

Python3里面字符串在内存中统一表现为Unicode编码

ASCII编码:英文字母,数字,一些符号,一共127字符(全部一个字节表示)

Unicode编码:解决出现乱码的问题(全世界所有语言一些字符都包含进来)是2-4个字节(16-32位),最少2个字节,Unicode编码是文本字符串在内存中的内在形式

utf-8编码表:unicode编码表的基础上做的优化,节省了空间(能够用一个字节表示就用一个字节,不能用1个字节就用多个字节表示)

gbk编码:用双字节编码表示中文和中文符号

编码与解码:

s ='我是tom'

encode:把Unicode编码转换为字节流(bytes)

a = s.encode('utf-8')

print(a)

decode:把字节流还原为Unicode

b = a.decode('utf-8')

print(b)

2、字符串常用内置方法

将单个字符转换为整数:

ord函数:print(ord('A'))

将整数转换为单个字符:

chr函数:print(chr(65))

字符串的赋值操作:

a_str ='cunyu'

字符串去空格和特殊字符

a_str = '  hello python '

lstrip:print(a_str.lstrip())   #去左边空格

rstrip:print(a_str.rstrip())  #去右边空格

strip:print(a_str.rstrip())   #去所有空格

b_str ='.hello python*'

print(b_str.lstrip('.'))   # 去除左边的符号

print(b_str.rstrip('*'))   #去除右边的符号

print(b_str.strip('.*'))  #去除所有符号

字符串的连接操作:

+:将两个字符串进行拼接

*:将字符串重复输出n次

a ='hello '           b ='python'

print(a+b)           print(a*3)

hello python        hello hello hello

替换操作(replace):

a_str = 'my name is tom'

a_str.replace('tom', 'marry')   # 第一个为替换部分,第二部分为替换的内容

查找(index,find):

a_str = 'my name is tom'

a_str.index('is')     # 查找is的索引,当找不到时会报错

a_str.find('is')    # 查找is的索引,当找不到时会返回-1

字符串的比较(<,<=,>,>=,==,!=): 比较的值为布尔

in:在什么里面

a_str = 'abcdef'

print('ab' in a_str)  # 判断ab是否在a_str里面

大小写转换(swapcase())

print('SIndy'.swapcase())   # 将大写转换为小写,小写转换为大写

首字母大写(capitalsize()):

print('sindy'.capitalsize())

标题化字符串(title()):

print('this is title'.title())

字符串切割(spilt):

print('my name is tom'.split(' '))   # 返回的是数组list[]

字符串的拼接(join):

print('+'.join(['my', 'name', 'is', 'tom']))    # join的参数为一个可迭代对象

对齐方式(ljust,rjust,center,zfill):

print('python'.ljust(10, '@'))   # 第一个参数为宽度,第二个参数为当字符串长度不够固定的宽度时的填充方式

print('python'.rjust(10, '@'))

print('python'.zfill(20))   # 填充,拉伸,不够的补0

print('python'.center(10, '@'))

判断以字符开始或结束(startswith,endswith):返回布尔

print('abcdef'.startswith('ab'))

print('abcdef'.endswith('ef'))

判断字符类型(isalnum,isalpha,isdigit,isspace,islower,issupper):

print('ab99'.isalnum())  # 数字或字符

print('abc'.isalpha())  # 全是字符

print('888'.isdigit())  # 全是数字

print(' '.isspace())  # 是否为空格

print('tom'.islower())  # 是否为小写

print('tom'.isupper())  # 是否为大写

统计某个字符或者某部分字符出现的次数(count):

print('abcdef'.count('bc'))

相关文章

  • 莫烦python基础教程

    python的基本使用 print 功能 print 字符串 python 中 print 字符串 要加’ ‘(单...

  • python变量类型

    字符串类型 字符串可以使用多种语法表示: python支持中文字符串,使用语法: 当python编译中文字符串出错...

  • 05. python3--字符串

    字符串 python中,字符串可使用单引号'包括,也可使用双引号"包括 python不支持单字符类型(char),...

  • execfile语句

    python2里的excefile语句可以像执行python代码那样使用字符串。不同的是exec使用字符串,而ex...

  • Lesson 014 —— python 字符串

    Lesson 014 —— python 字符串 字符串是 Python 中最常用的数据类型。我们可以使用引号('...

  • 使用多个分隔符分隔字符串

    python 多分隔符分隔字符串 python内建split方法不能使用多个分割符来分割字符串,可以使用re模块的...

  • python 基础 - 字符串

    python 字符串 字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串。创建字符...

  • python-学的不仅是技术

    Python 字符串 字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串。 创建字...

  • 6.Python基础数据类型之字符串

    Python基础数据类型之字符串 字符串(str)字符串是 Python 中最常用的数据类型。使用引号('或")来...

  • 006-字符串

    字符串 字符串是Python中最常用的数据类型。一般使用引号来创建字符串 使用单引号创建字符串'hello' 使用...

网友评论

      本文标题:Python字符串的使用

      本文链接:https://www.haomeiwen.com/subject/hbxngxtx.html