• python语法31[string的print和format]


    一 string与引号

    Python中的字符串可以使用四种引号来表示,‘(单引号),“(双引号),’‘'(三单引号),"""(三双引号)。 且他们均有相同的意思,需成对出现,单双引号可以相互嵌套。没有任何区别。

    >>> print('he said "good", you said "great", and i want to say """excellent"""')
    he said 
    "good", you said "great"and i want to say """excellent"""
     

    二 string的join和split函数 

    #string compare
    cmpStr = "abc"
    if cmpStr == "abc":
        
    print ("abc" + " Matches " + cmpStr)
    if cmpStr.upper() == 'abc'.upper():
        
    print (cmpStr + ' Matches ' + 'ABC'+ ' whit ignoring case')
    #abc Matches abc
    #
    abc Matches ABC whit ignoring case

    #string join
    wordList = ["A""few""more""good""words"]
    print ("List: " + ' '.join(wordList))
    #List: A few more good words

    #string split
    sentence = "A Simple Sentence."
    print (sentence.split())
    #['A', 'Simple', 'Sentence.']


    三 字符串的子串查找和替换

    #string find
    searchStr = "Red Blue Violet Green Blue Yellow Black"
    print (searchStr.find("Red"))
    print (searchStr.rfind("Blue"))
    print (searchStr.index("Blue"))
    print (searchStr.index("Blue",20))
    print (searchStr.rindex("Blue"))
    print (searchStr.rindex("Blue",1,18))

    f
    ='file.py'
    if f.endswith('.py'):
        
    print ("Python file: " + f)
    elif f.endswith('.txt'):
        
    print ("Text file: " + f)


    #string replace
    question = "What is the air speed velocity of an unlaiden swallow?"
    question2 
    = question.replace("swallow""European swallow")
    print(question2)

    对于子字符串的查找,可以使用in,可读性更好。

    if 'hello world,hello'.find('world'!= -1 : print('find')
    if 'world' in 'hello world,hello' : print('find')


    四 print函数中str的格式

    #string rjust and ljust
    chapters = {1:52:463:524:875:90}
    for x in chapters:
        
    print ("Chapter " + str(x) + str(chapters[x]).rjust(15,'.'))
    #Chapter 1..............5
    #Chapter 2.............46
    #Chapter 3.............52
    #Chapter 4.............87
    #Chapter 5.............90

    #print
    name='buddy'
    print("welcome" + " " + name + ", you are very handsome!" )
    print("welcome", name, ", you are very handsome!" )
    print('welcome %s, you are very handsome!' %name)
    #welcome buddy, you are very handsome!
    #welcome buddy , you are very handsome!
    #welcome buddy, you are very handsome!

    #print string format
    chapters2 = {1:52:463:524:875:90}
    for x in chapters2:
        
    print ("Chapter %d %15s" % (x,str(chapters2[x])))
    #Chapter 1               5
    #Chapter 2              46
    #Chapter 3              52
    #Chapter 4              87
    #Chapter 5              90

    print函数中使用%来隔离格式str和变量。

    五 使用str.format来格式字符串

    p4newuser = 'BBB'

    p4newuseremail 
    = 'BBB@example.com'
    p4newuserfullname 
    = 'BBB Frist'
    userspec 
    = str.format("\
    User: %s  \n\
    Email: %s  \n\
    Update:  \n\
    Access:  \n\
    FullName: %s \n\
    Password:  \n\
    " % (p4newuser,p4newuseremail,p4newuserfullname))

    print userspec

    结果:

    User: BBB
    Email: BBB@example.com
    Update:
    Access:
    FullName: BBB Frist
    Password:

     

     完!



    作者:iTech
    微信公众号: cicdops
    出处:http://itech.cnblogs.com/
    github:https://github.com/cicdops/cicdops

  • 相关阅读:
    Linux查看某个端口使用情况并kill
    CentOS 7.0关闭默认防火墙启用iptables防火墙
    Centos7配置JAVA_HOME
    Android Http请求方法汇总
    Android开发总结之 --- 定时,读写文件
    二维码,条形码
    SIM卡相关
    Eclipse快捷键
    easyui datagrid 表格不让选中(双层嵌套)
    easyui datagrid 三层嵌套
  • 原文地址:https://www.cnblogs.com/itech/p/1625008.html
Copyright © 2020-2023  润新知