4字符串

#使用转义符\
>>> print('What\'s your name?')
What's your name?

#或使用双引号
>>> print("What's your name?")
What's your name?


#三引号可打印多行
>>> words = """hello
world
abcd"""
>>> print(words)
hello
world
abcd
>>> py_str = 'python'             ###定义变量

>>> len(py_str)                   ###变量长度
6

>>> py_str[0]                     ###提取第一个字符
'p'

>>> py_str[-1]                    ###提取最后一个字符
'n'

>>> py_str[-2:]                   ###提取最后两个字符
'on'

>>> py_str[2:4]                  ###切片,提取出2到3,4是结尾
'th'

>>> py_str[::2]                  ###间隔为2,p-t-o-(python)
'pto'

>>> py_str[0] + py_str[-1]       ###提取第一和最后一个字符
'pn'

>>> py_str * 3                   ###重复三遍
'pythonpythonpython'
  • 分割
#定义变量
>>> yaya = '111:222:333:444:555:666:777:888:999'

以':'为分隔符
>>> yaya.split(':')
['111', '222', '333', '444', '555', '666', '777', '888', '999']

以':'为分隔符,只切出第一项
>>> yaya.split(':', 1)
['111', '222:333:444:555:666:777:888:999']

格式化

>>> "%s is %s years old" % ('bob', 23)
'bob is 23 years old'

%s    ///格式化字符串
%d    ///格式化整数

你可能感兴趣的:(4字符串)