字符串

python 字符串


str, ‘’, ‘’ ‘’, ‘’’ ‘’’;

python没有字符,只有字符串hh

切片

字符串不可以修改,修改的话,类似于tuple,

修改的话, 只可以整体修改

tuple 也是可这样,

确切的说只是修改了指针的指向

内容就是相当于我们之前的字符串常量, 不可以别修改, 只是可以改你指向的方向

函数请看转载

https://blog.csdn.net/marvin_wind/article/details/79903175

格式化字符串format

  • 简单应用

    “{} love {} {} much{}!”.format(“I”, “you”, “very”, ‘!’)

    “{0} love {1} {2} much{3}!”.format(“I”, “you”, “very”, ‘!’) ///0 1 2 是位置参数

    “{a} love {b} {c} much{d}!”.format(a = “I”, b = “you”, c = “very”, d = ‘!’)

    输出

    ‘I love you very much!!’

    “{3} love {2} {1} much{0}!”.format(“I”, “you”, “very”, ‘!’)

    输出

    ‘! love very you muchI!’

    “{{}}”.format(“1”)

    输出

    ‘{}’              #用于打印花括号,类似于我们之前的转义字符

  • 格式化输出
    在这里插入图片描述
    print("{} + {} = {}".format(1, 2, 1 + 2));
    print("{:0.1f}{}".format(14.551, ‘GB’));
    在这里插入图片描述

print("%c %c %c" % (97, 98, 99))
print("%d + %d = %d" % (1, 2, 1+2))
print("%o + %o = %o" % (9, 1, 1+9))
print("%x + %x = %X" % (10, 1, 1+10))
print("%f" % (14.332))
print("%e" % (14.332))
print("%E" % (14.332))

在这里插入图片描述

# 后面内容修改一点, 在八进制加上 0o, 十六进制 0X或0x

代码:

print("{a:04.1f} + {b:04.1f} = {c:06d}".format(a = 1, b = 2, c = 1+ 2 ))

输出

01.0 + 02.0 = 000003
原文地址:https://www.cnblogs.com/lucky-light/p/12983395.html