文章目录

  • 字符串
  • str
  • 转义字符
  • 转义字符表
  • 格式化
  • 传统格式化符
  • format()格式化
  • 数字格式化类型
  • str内置函数
  • 操作类函数



字符串

  • str
  • 转义符
  • 格式化
  • 内建函数

str

  • 表示文字信息,用‘单引号,双引号,三引号’表示

转义字符

  • 用特殊的方法表示一系列不方便写出的内容,比如回车,退格键,换行符等
  • 用反斜杠表示转移字符,要注意反斜杠后面的内容,可能表达的不是原来得含义
  • 在字符串中出现反斜杠时一定要注意
  • windows中表示换行为 \n
  • linex 中表示换行为 \r\n

转义字符表

  • \ (在行尾时)续行符
  • \\ 反斜杠符号
  • ’ 单引号
  • " 双引号
  • \a 响铃
  • \b 退格(Backspace)
  • \e 转义
  • \000 空
  • \n 换行 👍👍👍👍
  • \v 纵向制表符
  • \t 横向制表符
  • \r 回车
  • \f 换页
    -\oyy 八进制数,yy代表的字符,例如:\o12代表换行
  • \xyy 十六进制数,yy代表的字符,例如:\x0a代表换行
  • \other 其它的字符以普通格式输出
    #表示转移字符实例
    a = ‘Let’s go’ #此中的’就表示’的意思,\做转义作用
    print(a)

    #双反斜杠代表单反斜杠的含义(\)
    b = ‘LOVE \ ME’
    print(b)

    Let’s go
    LOVE \ ME

格式化

  • 定义:把字符串按一定的格式打印或填充
  • 传统格式化
  • 使用%(百分号)占位也叫占位符
  • format()

传统格式化符

  • %d 十进制有符号整数
  • %u 十进制无符号整数
  • %f 浮点数
  • %s 字符串
  • %c 单个字符
  • %p 指针的值
  • %e 指数形式的浮点数
  • %x, %X 无符号以十六进制表示的整数
  • %0 无符号以八进制表示的整数
  • %g 自动选择合适的表示法
    #传统格式化符类型1
    a = ‘I love %s’
    print(a%‘生活’) #直接在a后加%(占位符)和字符串

    #传统格式化符类型2
    print(‘I Love %s’%‘人民币’)

    #传统格式化类型3
    a = ‘I love %s or I love %s’
    print(a%(‘生活’,‘人民币’)) #输出信息大于一个时,可用括号写入匹对
    I love 生活
    I Love 人民币
    I love 生活 or I love 人民币

format()格式化

  • 使用函数形式进行格式化,从而代替传统格式。
#format示例1
a = 'l love {}'
print(a.format('音乐'))
#format示例2
a = 'I love {}'.format('生活')
print(a)
#format示例3  #format可指定位置
a = '{0} {1}'
print(a.format('hello','world'))
#format示例4  #format可指定位置
a = '{1} {0}'
print(a.format('hello','world'))
#format错误示例
a = 'I like {} and like {}'
print(a.format('编程')) #字符中初始设置两个空,format括号内中只输入一个,则会报错


#结果:
l love 音乐
I love 生活
hello world
world hello
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-28-ca23d2099f16> in <module>()
     13 #format错误示例
     14 a = 'I like {} and like {}'
---> 15 print(a.format('编程')) #字符中初始设置两个空,format括号内中只输入一个,则会报错

IndexError: tuple index out of range
#format括号内设置命名参数
a = '我是{zhiwei},来自{didian},工作于{gongsi}'
print(a.format(zhiwei = '程序员',didian ='北京',gongsi ='阿里巴巴')) 

#结果:
我是程序员,来自北京,工作于阿里巴巴


#format格式中运用字典,且需解析操作
#其中(\表示续行符)
dict_style = {'zhiwei':'程序员',\
             'didian':'北京',\
             'gongsi':'阿里巴巴'}
print(a.format(**dict_style))  # **两个星号表示解析
我是程序员,来自北京,工作于阿里巴巴
#对数字进行格式化
s = 'my weight is {:.2f}kg,my height is {:.2f}cm'
print(s.format(80.163,182.156))

#结果:
my weight is 80.16kg,my height is 182.16cm

数字格式化类型

  • ^,>,<分别表示剧中,右对齐,左对齐
  • :(冒号)后面填充字符,只能填充一个字符,不填则默认为空格
  • +,表示正数前面显示正号
  • b,d,o,x在冒号后面填充的字符,分别是二进制,十进制,八进制,十六进制
  • {}大括号可用大括号转义{ { } }
#进制转化
a = 8
print('{:b}'.format(a))

#结果:
1000

str内置函数

  • 内容查找类
  • find 在总字符串中查找指定字符的位置,并返回位置,若查找到返回True,查不到返回-1
  • rfind表示为从左边开始查找
  • index与find函数相似,区别在于查找不到时返回False
  • 内容判断类
  • islower/isupper 判断字符串中的字母是否’只’为’小写’/‘大写’,(空格不影响判断)
  • isalpha 判断字符串中是否’只’存在’字母’(不包括空格,标点符号等),汉字被认为是alpha,区分中英文可使用unicode
  • isdigit,isnumeric,isdecimal 判断字符串是否’只’为’数字’。isnumeric可判断汉字化数字,其他不行
  • startswith/endswith 判断总字符串中是否以指定的’字符串或字母’开头或结尾,并返回True或False
#find案例
music = 'music'
code = 'codd'
s = 'l love music and code'
print(s.find(code)) #返回m所在的位置为7
#index案例
music = 'music'
code = 'codd'
s = 'l love music and code'
print(s.index(code))

#结果:
-1
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-61-12f286b7520d> in <module>()
      8 code = 'codd'
      9 s = 'l love music and code'
---> 10 print(s.index(code))

ValueError: substring not found
#islower案例,判断字符串中字母是否'只为'小写
s1 = 'ABC'
s2 = 'aBc'
s3 = 'abc'
print(s1.islower())
print(s2.islower())
print(s3.islower())
#isalpha案例
s1 = 'ABC'
s2 = '123'
s3 = 'A1C'
print(s1.isalpha())
print(s2.isalpha())
print(s3.isalpha())
#isdigit案例
s1 = 'ABC'
s2 = '123'
s3 = 'A1C'
print(s1.isdigit())
print(s2.isdigit())
print(s3.isdigit())

#结果:
False
False
True
True
False
False
False
True
False
#startswith和endswith
You = 'You'
programming = 'programming'
s = 'You and I love programming'
print(s.startswith(You))
print(s.endswith(programming))

#结果:
True
True

操作类函数

  • format()格式化字符串
  • strip 指定删除字符串’两边’的字符或字符串,默认为删除空格。此外注意此处删除可能不止一个,而是删除符合指定要求,从头开始的连续字符
  • rstrip/lstrip 分别为从右边/左边开始删除
  • join 此函数可对字符串进行拼接,且需要一个可迭代的内容为参数,对其内容中各个字符串用指定的字符进行拼接
#strip案例
s = 'play the basketball '
print(s.strip(),end = '-----') #此处效果为删除空格
#strip案例
print()
print(s.rstrip('l')) #删除右边的连续字符l
#注意:如果指定删除某个字符时,则默认的空格就不会删去
s = 'play the basketball '
print(s.strip('p'),end = '-----')
play the basketball-----
play the basketball 
lay the basketball -----
#join案例
s = '%'
l = '¥'
o = '&'
list = ['python','java','go','c++',]
print(s.join(list))
print(l.join(list))
print(o.join(list))

#结果:
python%java%go%c++
python¥java¥go¥c++
python&java&go&c++