zoukankan      html  css  js  c++  java
  • 014 数据类型:字符串类型

    一、字符串(str)

    1.1 什么是字符串

    ​ 字符串就是字符组成的串,就好像羊肉串一样。

    1.2 定义方法

    • ' ' 单引号 和 “ “ 双引号 和 ‘’‘ ’‘’ 三引号 都可以表示字符串
    name1 = "xucheng"
    print(id(name1))
    print(type(name1))
    print(name1)
    

    ​ 输出:

    ​ 4418849624

    ​ <class 'str'>
    ​ xucheng

    name3 = """xu
    cheng"""
    
    print(name3)
    

    ​ 输出:

    ​ xu

    ​ cheng

    1.3 使用方法

    字符串只能+、*和逻辑比较

    字符串的拼接,即重新申请一个小空间把两个字符串都拷贝一份后再拼接。而不是你YY的把一个小空间内的变量值复制到另一个变量的小空间内,然后拼接。

    msg2 = "my name is 'xucheng'"
    msg3 = 'my name is "xucheng"'
    
    print(msg2 + msg3)  # my name is 'xucheng'my name is "xucheng"
    

    ​ 输出:my name is 'xucheng'my name is "xucheng"

    注意:如果字符串内有引号,则包裹字符串的引号和字符串内部的引号不能相同。

    name = 'xucheng '
    print(name * 5)
    

    ​ 输出:xucheng xucheng xucheng xucheng xucheng

    注意:字符串的乘法只能乘以数字。

    msg1 = 'hello'
    msg2 = 'z'
    
    print(msg1 > msg2)
    

    ​ 输出:False

    注意:字符串比较大小,按照ASCII码比较,以后会细讲。

    msg3 = 'zero'
    msg4 = 'zx'
    
    print(msg3 > msg4)
    print('Z' > 'A')
    print('Z' > 'a')
    False
    True
    False
    

    ​ 输出:

    ​ False
    ​ True
    ​ False

    注意:字符串的比较是字母的顺序。

  • 相关阅读:
    OpenState: Programming Platform-independent Stateful OpenFlow Applications Inside the Switch
    带状态论文粗读(二)
    In-band Network Function Telemetry
    基于微信小程序的失物招领系统的Postmortem
    OpenStack安装
    Alpha冲刺Day12
    冲刺合集
    Alpha冲刺Day11
    Alpha冲刺总结
    测试总结
  • 原文地址:https://www.cnblogs.com/XuChengNotes/p/11271462.html
Copyright © 2011-2022 走看看