生成字符串

Python中可以使用一对单引号''或者双引号""生成字符串。

s = "hello, world"
print s

hello, world

本人对于Python学习创建了一个小小的学习圈子,为各位提供了一个平台,大家一起来讨论学习Python。欢迎各位到来Python学习群:923414804一起讨论视频分享学习。Python是未来的发展方向,正在挑战我们的分析能力及对世界的认知方式,因此,我们与时俱进,迎接变化,并不断的成长,掌握Python核心技术,才是掌握真正的价值所在。
s = 'hello world'
print s

hello world

简单操作

加法:

s = 'hello ' + 'world'
s

'hello world'

字符串与数字相乘:

"echo" * 3

'echoechoecho'

字符串长度:

len(s)

11

字符串方法

Python是一种面向对象的语言,面向对象的语言中一个必不可少的元素就是方法,而字符串是对象的一种,所以有很多可用的方法。

跟很多语言一样,Python使用以下形式来调用方法:

对象.方法(参数)

分割

s.split()将s按照空格(包括多个空格,制表符\t,换行符\n等)分割,并返回所有分割得到的字符串。

line = "1 2 3 4  5"
numbers = line.split()
print numbers

['1', '2', '3', '4', '5']

s.split(sep)以给定的sep为分隔符对s进行分割。

line = "1,2,3,4,5"
numbers = line.split(',')
print numbers

['1', '2', '3', '4', '5']

连接

与分割相反,s.join(str_sequence)的作用是以s为连接符将字符串序列str_sequence中的元素连接起来,并返回连接后得到的新字符串:

s = ' '
s.join(numbers)

'1 2 3 4 5'

s = ','
s.join(numbers)

'1,2,3,4,5'

替换

s.replace(part1, part2)将字符串s中指定的部分part1替换成想要的部分part2,并返回新的字符串。

s = "hello world"
s.replace('world', 'python')

'hello python'

此时,s的值并没有变化,替换方法只是生成了一个新的字符串。

s

'hello world'

大小写转换

s.upper()方法返回一个将s中的字母全部大写的新字符串。

s.lower()方法返回一个将s中的字母全部小写的新字符串。

"hello world".upper()

'HELLO WORLD'

这两种方法也不会改变原来s的值:

s = "HELLO WORLD"
print s.lower()
print s

hello world
HELLO WORLD

去除多余空格

s.strip()返回一个将s两端的多余空格除去的新字符串。

s.lstrip()返回一个将s开头的多余空格除去的新字符串。

s.rstrip()返回一个将s结尾的多余空格除去的新字符串。

s = "  hello world   "
s.strip()

'hello world'

s的值依然不会变化:

s

' hello world '

s.lstrip()

'hello world '

s.rstrip()

' hello world'

更多方法

可以使用dir函数查看所有可以使用的方法:

dir(s)

['add',
'class',
'contains',
'delattr',
'doc',
'eq',
'format',
'ge',
'getattribute',
'getitem',
'getnewargs',
'getslice',
'gt',
'hash',
'init',
'le',
'len',
'lt',
'mod',
'mul',
'ne',
'new',
'reduce',
'reduce_ex',
'repr',
'rmod',
'rmul',
'setattr',
'sizeof',
'str',
'subclasshook',
'_formatter_field_name_split',
'_formatter_parser',
'capitalize',
'center',
'count',
'decode',
'encode',
'endswith',
'expandtabs',
'find',
'format',
'index',
'isalnum',
'isalpha',
'isdigit',
'islower',
'isspace',
'istitle',
'isupper',
'join',
'ljust',
'lower',
'lstrip',
'partition',
'replace',
'rfind',
'rindex',
'rjust',
'rpartition',
'rsplit',
'rstrip',
'split',
'splitlines',
'startswith',
'strip',
'swapcase',
'title',
'translate',
'upper',
'zfill']

多行字符串

Python 用一对 """ 或者 ''' 来生成多行字符串:

a = """hello world.
it is a nice day."""
print a

hello world.
it is a nice day.

在储存时,我们在两行字符间加上一个换行符 '\n'

a

'hello world.\nit is a nice day.'

使用 () 或者 \ 来换行

当代码太长或者为了美观起见时,我们可以使用两种方法来将一行代码转为多行代码:

  • ()
  • \
a = ("hello, world. "
    "it's a nice day. "
    "my name is xxx")
a

"hello, world. it's a nice day. my name is xxx"

a = "hello, world. " \
    "it's a nice day. " \
    "my name is xxx"
a

"hello, world. it's a nice day. my name is xxx"

强制转换为字符串

  • str(ob)强制将ob转化成字符串。
  • repr(ob)也是强制将ob转化成字符串。

不同点如下:

str(1.1 + 2.2)

'3.3'

repr(1.1 + 2.2)

'3.3000000000000003'

整数与不同进制的字符串的转化

可以将整数按照不同进制转化为不同类型的字符串。

十六进制:

hex(255)

'0xff'

八进制:

oct(255)

'0377'

二进制:

bin(255)

'0b11111111'

可以使用 int 将字符串转为整数:

int('23')

23

还可以指定按照多少进制来进行转换,最后返回十进制表达的整数:

int('FF', 16)

255

int('377', 8)

255

int('11111111', 2)

255

float 可以将字符串转换为浮点数:

float('3.5')

3.5

格式化字符串

Python用字符串的format()方法来格式化字符串。

具体用法如下,字符串中花括号 {} 的部分会被format传入的参数替代,传入的值可以是字符串,也可以是数字或者别的对象。

'{} {} {}'.format('a', 'b', 'c')

'a b c'

可以用数字指定传入参数的相对位置:

'{2} {1} {0}'.format('a', 'b', 'c')

'c b a'

还可以指定传入参数的名称:

'{color} {n} {x}'.format(n=10, x=1.5, color='blue')

'blue 10 1.5'

可以在一起混用:

'{color} {0} {x} {1}'.format(10, 'foo', x = 1.5, color='blue')

'blue 10 1.5 foo'

可以用{<field name>:<format>}指定格式:

from math import pi

'{0:10} {1:10d} {2:10.2f}'.format('foo', 5, 2 * pi)

'foo 5 6.28'

具体规则与C中相同。

也可以使用旧式的 % 方法进行格式化:

s = "some numbers:"
x = 1.34
y = 2
# 用百分号隔开,括号括起来
t = "%s %f, %d" % (s, x, y)
t

'some numbers: 1.340000, 2'

本人对于Python学习创建了一个小小的学习圈子,为各位提供了一个平台,大家一起来讨论学习Python。欢迎各位到来Python学习群:923414804一起讨论视频分享学习。Python是未来的发展方向,正在挑战我们的分析能力及对世界的认知方式,因此,我们与时俱进,迎接变化,并不断的成长,掌握Python核心技术,才是掌握真正的价值所在。

posted on 2019-01-14 13:55  派森学python  阅读(413)  评论(0)    收藏  举报