Python基础: 容器

  • 原创
  • Madman
  • /
  • /
  • 0
  • 7565 次阅读

Python容器-min.png

Synopsis: 字符串是Python中最常用的数据类型,我们可以使用单引号'或双引号"来创建字符串。input函数获取的输入数据全是字符串类型。字符串对象是不可变对象,无法修改内容,所谓"修改"只是创建新的字符串对象,并把原对象引用指向该新字符串对象。一旦创建了元组,则不能再修改元组的元素,包括不能删除其中的元素

1. 字符串

字符串是Python中最常用的数据类型,我们可以使用单引号'或双引号"来创建字符串。input函数获取的输入数据全是字符串类型。

1.1 字符串运算符

字符串对象是不可变对象,无法修改内容,所谓"修改"只是创建新的字符串对象,并把原对象引用指向该新字符串对象。

操作符 描述 实例 a='Hello', b='Python'
+ 字符串连接 a+b输出: HelloPython
* 重复输出字符串 a*2输出: HelloHello
[] 通过索引获取字符串中字符 a[1]输出: e
[:] 截取字符串的一部分 a[1:3]输出: el
in 成员运算符 - 如果字符串中包含给定的字符返回 True 'H' in a输出: True
not in 成员运算符 - 如果字符串中不包含给定的字符返回 True 'A' not in a输出: True
r/R 原始字符串 - 原始字符串:所有的字符串都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符。 原始字符串除在字符串的第一个引号前加上字母"r"(可以大小写)以外,与普通字符串有着几乎完全相同的语法。 print(r'\n')输出: \n

1.2 字符串格式化

建议使用新的字符串格式化函数str.format(),通过{}:来代替以前的%。format函数可以接受任意个数的参数,位置也可以不按顺序。

In [1]: "{} {}".format("Hello", "World")
Out[1]: 'Hello World'

In [2]: "{0} {1}".format("Hello", "World")
Out[2]: 'Hello World'

In [3]: person = {'name': 'wangy', 'age': 18}

In [4]: print("姓名: {name}, 年龄: {age}".format(**person))
姓名: wangy, 年龄: 18

In [5]: learn = ['c', 'python']

In [6]: print("静态语言: {0[0]}, 动态语言: {0[1]}".format(learn))
静态语言: c, 动态语言: python

数字格式化:

  • ^, <, >分别是居中左对齐右对齐,后面是宽度,:冒号后面是填充的字符,只能是一个字符,不指定则默认是用空格填充。
  • +表示在正数前显示+,负数前显示-
  • (空格)表示在正数前加空格
  • bdox分别是二进制、十进制、八进制、十六进制
  • 使用大括号{{}}来转义大括号
In [1]: a = 'Hello'

In [2]: print('变量a的值: {},它的对应位置是 {{0}}'.format(a))
变量a的值: Hello,它的对应位置是 {0}

1.3字符串内建函数

(1) 字母大小写

S.upper()

返回字符串中所有字母的大写形式。

In [1]: a = 'hello'

In [2]: a.upper()
Out[2]: 'HELLO'

S.lower()

返回字符串中所有字母的小写形式。

In [1]: a = 'HELLO'

In [2]: a.lower()
Out[2]: 'hello'

S.swapcase()

将字符串中的字母大小写互换。

In [1]: a = 'Hello'

In [2]: a.swapcase()
Out[2]: 'hELLO'

S.capitalize()

整个字符串中只有第一个单词的首字母大写。

In [1]: a = 'hello world'

In [2]: a.capitalize()
Out[2]: 'Hello world'

S.title()

整个字符串中所有单词的首字母都大写。

In [1]: a = 'hello world'

In [2]: a.title()
Out[2]: 'Hello World'

(2) 格式化

S.ljust(width[, fillchar])

获取固定长度,左对齐,右边不够默认用空格补齐。

In [1]: a = 'hello'

In [2]: a.ljust(10)
Out[2]: 'hello     '

S.rjust(width[, fillchar])

获取固定长度,右对齐,左边不够默认用空格补齐。

In [1]: a = 'hello'

In [2]: a.rjust(10)
Out[2]: '     hello'

S.center(width[, fillchar])

获取固定长度,中间对齐,两边不够默认用空格补齐。

In [1]: a = 'hello'

In [2]: a.center(10)
Out[2]: '  hello   '

S.zfill(width)

获取固定长度,右对齐,左边不足用0补齐。

In [1]: a = 'hello'

In [2]: a.zfill(10)
Out[2]: '00000hello'

(3) 搜索

S.find(sub[, start[, end]])

在字符串中搜索子串第一次出现的位置(可以指定搜索范围,默认是整个字符串),如果搜索不到,则返回-1

In [1]: a = 'hello world, hello python'

In [2]: a.find('hello')
Out[2]: 0

S.find()可以用S.index(sub[, start[, end]])替代,只是如果没有查找到子串时,S.index()会抛异常ValueError

index(...)
    S.index(sub[, start[, end]]) -> int

    Like S.find() but raise ValueError when the substring is not found.

S.rfind(sub[, start[, end]])

在字符串中搜索子串最后一次出现的位置(可以指定搜索范围,默认是整个字符串),相当于从右边开始查找。如果搜索不到,则返回-1

In [1]: a = 'hello world, hello python'

In [2]: a.rfind('hello')
Out[2]: 13

S.rfind()可以用S.rindex(sub[, start[, end]])替代,只是如果没有查找到子串时,S.rindex()会抛异常ValueError

S.count(sub[, start[, end]])

在字符串中搜索子串总共出现多少次(可以指定搜索范围,默认是整个字符串),如果搜索不到,则返回0

In [1]: a = 'hello world, hello python'

In [2]: a.count('hello')
Out[2]: 2

(4) 替换

S.replace(old, new[, count])

在字符串中将old子串替换为new子串,默认替换掉所有old子串

In [1]: a = 'hello world, hello python'

In [2]: a.replace('hello', '你好')
Out[2]: '你好 world, 你好 python'

In [3]: a.replace('hello', '你好', 1)
Out[3]: '你好 world, hello python'

S[::-1]

反转字符串

In [1]: a = 'hello world'

In [2]: a[::-1]
Out[2]: 'dlrow olleh'

(5) 移除字符串头尾指定的字符

默认移除空白符(包括\n, \r, \t, )

S.strip([chars])

移除字符串的首尾字符

In [1]: a = '  \t hello world \r\n '

In [2]: a.strip()
Out[2]: 'hello world'

In [3]: a = '---hello world---'

In [4]: a
                                
                            
  • shishijia
  • fissh
  • jerry
  • john
  • tiffaniamber
  • chloelynn
  • dudley
  • caitlen
  • theresita
  • lilien
  • 60505186
  • truly
  • juneau
  • brailey
  • jolijn
  • lilla
  • reggie
  • waunda
  • elder
  • derion
  • siane
  • kejuana
  • karmelo
  • lilleeana
未经允许不得转载: LIFE & SHARE - 王颜公子 » Python基础: 容器

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列