Python 字符串 - Python零基础入门教程

痛定思痛。 2022-10-14 10:51 381阅读 0赞

目录

  • 一.前言
  • 二.Python 字符串运算符
  • 三.Python 字符串构造
  • 四.Python 字符串截取
  • 五.Python 字符串替换 – replace()方法
  • 六.Python 字符串大小写
  • 七.猜你喜欢

零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门

一.前言

Python 变量 文章中我们对 整数 / 浮点数 / bool 值有做过讲解,今天详细在讲解一下关于字符串的内容,字符串俗称:str

在本文会大量的使用 print 和 format 函数,如果还有不太熟悉使用的盆友,请先跳转:

  • Python print 函数
  • Python format 函数

二.Python 字符串运算符

介绍两个关于 Python 字符串的运算符,innot in,主要用于检测字符串中是否存在某个字符或者字符串,如果存在返回 True,不存在返回 False,直接上代码演示:

  1. # !usr/bin/env python
  2. # -*- coding:utf-8 _*-
  3. """
  4. @Author:猿说编程
  5. @Blog(个人博客地址): www.codersrc.com
  6. @File:python字符串str使用.py
  7. @Time:2021/3/21 23:00
  8. @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
  9. """
  10. # 检测单个字符
  11. str1 = "hello world"
  12. if "h" in str1:
  13. print("{} 字符串包含 'h'".format(str1)) # 注意单引号和双引号的配合使用
  14. else:
  15. print("{} 字符串不包含 'h'".format(str1))
  16. # 检测字符串
  17. if "hello" in str1:
  18. print("{} 字符串包含 'hello'".format(str1)) # 注意单引号和双引号的配合使用
  19. else:
  20. print("{} 字符串不包含 'hello'".format(str1))
  21. # 使用 not in
  22. if "hllo" not in str1:
  23. print("{} 字符串不包含 'hllo'".format(str1)) # 注意单引号和双引号的配合使用
  24. else:
  25. print("{} 字符串包含 'hllo'".format(str1))
  26. '''
  27. 输出结果:
  28. hello world 字符串包含 'h'
  29. hello world 字符串包含 'hello'
  30. hello world 字符串不包含 'hllo'
  31. '''

三.Python 字符串构造

字符串可以直接拼接,同样也可以使用 format 函数或者 % 符号构造,代码如下:

  1. # !usr/bin/env python
  2. # -*- coding:utf-8 _*-
  3. """
  4. @Author:猿说编程
  5. @Blog(个人博客地址): www.codersrc.com
  6. @File:python字符串str使用.py
  7. @Time:2021/3/21 23:00
  8. @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
  9. """
  10. str1 = "hello world"
  11. print("%s 字符串总长度:%d" % (str1,len(str1))) # len()获取字符串长度
  12. #方法一:
  13. for i in str1:
  14. print(i,end="-") # print 函数默认换行,强制将换行符改为 '-',可以改为任意字符
  15. print("\n") # "\n" 表示换行
  16. print("*"*20)
  17. #方法二:
  18. for i in range(0,len(str1)):
  19. print(str1[i],end=' ') # 每个字符以空格隔开
  20. print("\n") # "\n" 表示换行
  21. print("*"*20)
  22. #方法三:
  23. a = 0
  24. while a < len(str1):
  25. print("str[%d] = %s " % (a,str1[a]))
  26. a += 1
  27. print("程序结束,退出程序")
  28. '''
  29. 输出结果:
  30. hello world 字符串总长度:11
  31. h-e-l-l-o- -w-o-r-l-d-
  32. ********************
  33. h e l l o w o r l d
  34. ********************
  35. str[0] = h
  36. str[1] = e
  37. str[2] = l
  38. str[3] = l
  39. str[4] = o
  40. str[5] =
  41. str[6] = w
  42. str[7] = o
  43. str[8] = r
  44. str[9] = l
  45. str[10] = d
  46. 程序结束,退出程序
  47. '''

四.Python 字符串截取

字符串中的每一个字符都有一个默认的索引值,从左到右默认重 0 开始,依次递增;从右往左默认重-1 开始,依次递增;

2f139764d688d8984f443feb15f3cf04.png

  1. # !usr/bin/env python
  2. # -*- coding:utf-8 _*-
  3. """
  4. @Author:猿说编程
  5. @Blog(个人博客地址): www.codersrc.com
  6. @File:python字符串str使用.py
  7. @Time:2021/3/21 23:00
  8. @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
  9. """
  10. str1 = "猿说python"
  11. print(len(str1)) # 内置函数 len() 获取字符串长度
  12. print(str1) # 打印字符串
  13. print(str1[2]) # 获取字符串中的第二个字符
  14. print(str1[0:2]) # 截取字符串索引值为0~1的字符,不包括索引值为2的字符
  15. print(str1[2:5]) # 截取字符串索引值为2~4的字符,不包括索引值为5的字符
  16. print(str1[2:-1]) # 截取字符串重索引值为2开始直到字符串结尾的前一个,-1的索引值表示最后一个
  17. print(str1[2:len(str1)]) # 截取字符串索引值2~8,最后一个字符的索引值为7,所以刚刚好能截取到字符串末尾
  18. # 截取在列表中索引值为0-4的数据,冒号前面不设置参数,默认重0开始,注意截取并不包括4
  19. print(str1[:4])
  20. # 截取在列表中索引值为2-末尾的数据,冒号后面不设置参数,默认截取到最后一位数据,注意截取包括最后一位
  21. print(str1[2:])
  22. print("程序结束,退出程序")
  23. '''
  24. 输出结果:
  25. 8
  26. 猿说python
  27. p
  28. 猿说
  29. pyt
  30. pytho
  31. python
  32. 猿说py
  33. python
  34. 程序结束,退出程序
  35. '''

注意:在上面 print(str1[2:-1]) 该行代码中,-1 表示最后一位字符串索引,但是截取的范围并不包括字符串的最后一位。

五.Python 字符串替换 – replace()方法

  1. '''
  2. 函数介绍:替换字符串中指定的内容,并返回新的字符串
  3. old:字符串中需要被替换的字符或者字符串(旧字符串,原本一直就在字符串)
  4. new:替换之后的内容(新字符串,添加到字符串代替old的内容)
  5. '''
  6. str.replace(old, new)

示例代码如下:

  1. # !usr/bin/env python
  2. # -*- coding:utf-8 _*-
  3. """
  4. @Author:猿说编程
  5. @Blog(个人博客地址): www.codersrc.com
  6. @File:python字符串str使用.py
  7. @Time:2021/3/21 23:00
  8. @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
  9. """
  10. str1 = "hello world"
  11. str1 = str1.replace("hello","猿说PYTHON")
  12. print(str1)
  13. str1 = "hello world"
  14. str1 = str1.replace("world","python 教程")
  15. print(str1)
  16. '''
  17. 输出结果:
  18. 猿说PYTHON world
  19. hello python 教程
  20. '''

六.Python 字符串大小写

对字符串进行大小写转换处理,常用的内置函数如下:

  • upper:把所有字符中的小写字母转换成大写字母
  • lower:把所有字符中的大写字母转换成小写字母
  • capitalize:把第一个字母转化为大写字母,其余小写
  • title:把每个单词的第一个字母转化为大写,其余小写

    1. # !usr/bin/env python
    2. # -_- coding:utf-8 \__-
    3. """
    4. @Author:猿说编程
    5. @Blog(个人博客地址): www.codersrc.com
    6. @File:python 字符串 str 使用.py
    7. @Time:2021/3/21 23:00
    8. @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
    9. """
    10. str = "www.shuopython.com"
    11. print(str.upper()) # 把所有字符中的小写字母转换成大写字母
    12. print(str.lower()) # 把所有字符中的大写字母转换成小写字母
    13. print(str.capitalize()) # 把第一个字母转化为大写字母,其余小写
    14. print(str.title()) # 把每个单词的第一个字母转化为大写,其余小写
    15. '''
    16. 输出结果:
    17. WWW.SHUOPYTHON.COM
    18. www.shuopython.com
    19. Www.shuopython.com
    20. Www.Shuopython.Com
    21. Process finished with exit code 0
    22. '''

关于字符串的函数还有很多,由于篇幅有限,后面的文章我们继续讲解更多关于Python 字符串相关函数。

七.猜你喜欢

  1. Python 简介
  2. Python Pycharm Anacanda 区别
  3. Python2.x 和 Python3.x,如何选择?
  4. Python 配置环境
  5. Python Hello World 入门
  6. Python 代码注释
  7. Python 中文编码
  8. Anaconda 是什么?Anconda 下载安装教程
  9. Pycharm 提示:this license **** has been cancelled
  10. Pycharm 设置开发模板/字体大小/背景颜色

未经允许不得转载:猿说编程 » Python 字符串

发表评论

表情:
评论列表 (有 0 条评论,381人围观)

还没有评论,来说两句吧...

相关阅读