美文网首页Python
Python基础(2) - 字符串使用

Python基础(2) - 字符串使用

作者: xianling_he | 来源:发表于2020-02-18 20:58 被阅读0次

    如何使用转义符

    • 如何显示出单引号
      如果要输出单引号,就需要用双引号("")将字符串括起来输出显示
    print("hello 'world'")
    
    • 如何显示出双引号
      反之如果要输出双引号,就需要用单引号('')将字符串括起来输出显示
    print('hello "world"')
    
    • 如何同时在字符串中显示单引号和双引号
      这个时候需要加上转义符()进行区分单,双引号,这样就可以同时输出单引号及双引号显示
    print('"hello" \'world\' ')
    

    如何让转义符失效

    • 如何让字符串中的转义符失效方法如下:
    • r
    print(r'Let \'s talk!')
    
    • repr
    print(repr('Let \'s talk'))
    print(repr('hello \n world'))
    
    • \
    print('hello \\n world')
    

    保持字符串的原始格式

    • 如何保留字符串中的原始格式
      可以使用三个单引号(''') 或者双引号(""")
    print('''
    please 
      talk 
        with 
          me
    ''')
    
    print("""
    please 
      talk 
        with 
          me
    """)
    

    总结:
    1.使用转义符可以输出任何字符,包括双、单引号
    2.通过三种方式(在字符串前加入r,repr 和 反斜杠())可以让转义符失效
    3.用三对单引号或者双引号可以让字符串保留原始格式

    he.xianling加油~

    相关文章

      网友评论

        本文标题:Python基础(2) - 字符串使用

        本文链接:https://www.haomeiwen.com/subject/nlunfhtx.html