zoukankan      html  css  js  c++  java
  • Python-字符串

    strip   移除空白

    name = '    changke'
    print(name)
    print(name.strip())

    len     显示字符串长度

    name = 'changke'
    print(len(name))#显示字符串的长度

    索引

    name = 'changke'
    print(name[2])#输出name的下标为2的字符串

    切片

    name = 'changke'
    print(name[0:5])#输出'chang',切片的原则为含头不含尾

    用+拼接

    name = 'changke'
    age = '24'
    #我们想打印出'my name is changke and i am 24 years old',可以用拼接的方式
    msg = 'my name is ' + name + ' and i am ' + age + ' years old'
    print(msg)
    #使用+拼接字符串时会增加调用内存的次数,影响速度

    格式化输出

    name = 'changke'
    age = '24'
    #我们想打印出'my name is changke and i am 24 years old'
    msg = 'my name is %s and i am %syears old'%(name,age)
    print(msg)
    #   %为占位符
    captalize:首字母大写
    name = 'changke'
    print(name.capitalize()) #首字母大写

    swapcase:大小写翻转

    name = 'changke'
    print(name.swapcase()) #大小写翻转

    每个单词的首字母大写

    msg='my name is changke'
    print(msg.title()) #每个单词的首字母大写

    后续更新......

  • 相关阅读:
    数位dp
    可持久化Trie
    网络流
    欧拉定理
    点、边双,圆方树
    [USACO5.3]窗体面积Window Area
    6.2三道模拟
    BZOJ2054 疯狂的馒头
    [USACO5.1]夜空繁星Starry Night
    [USACO5.1]乐曲主题Musical Themes
  • 原文地址:https://www.cnblogs.com/matrixchang/p/8340742.html
Copyright © 2011-2022 走看看