python中的string类

python中的string数据类型

  1. string类的操作
    • string 可以直接相加,作用相当于字符拼接
    • string 可以利用与 integer 相乘的方法进行复制。
>>>"egg" + "chicken"
eggchicken

>>>4*'egg'
eggeggeggegg
>>>2*'23'
'2323'
>>>int('2323')#类型转换
2323


  • 格式化输出. 可以用上述+的方式进行拼接进行输出, 也可以用格式化的输出方式
#example 1, 单输出
name = "Mike"
print "Hello %s" % (name)

#example 2  多输出
string_1 = "Camelot"
string_2 = "place"

print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)
  • xxx.upper()此类dot notation只能对于string类型的数据使用; upper(xxx)可以用于其他数据类型. 注意.len()没有 dot notation的用法.

猜你喜欢

转载自blog.csdn.net/qq_29007291/article/details/79186780