目录

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

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

一.前言

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

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

  • Python print 函数
  • Python format 函数

二.Python 字符串运算符

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

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:python字符串str使用.py
@Time:2021/3/21 23:00
@Motto:不积跬步无以至千里,不积小流无以成江海	,程序人生的精彩需要坚持不懈地积累!

"""

# 检测单个字符
str1 = "hello world"
if "h" in str1:
    print("{} 字符串包含 'h'".format(str1))  # 注意单引号和双引号的配合使用
else:
    print("{} 字符串不包含 'h'".format(str1))

# 检测字符串
if "hello" in str1:
    print("{} 字符串包含 'hello'".format(str1))  # 注意单引号和双引号的配合使用
else:
    print("{} 字符串不包含 'hello'".format(str1))

# 使用 not in
if "hllo" not in str1:
    print("{} 字符串不包含 'hllo'".format(str1))  # 注意单引号和双引号的配合使用
else:
    print("{} 字符串包含 'hllo'".format(str1))


'''
输出结果:

hello world 字符串包含 'h'
hello world 字符串包含 'hello'
hello world 字符串不包含 'hllo'

'''

三.Python 字符串构造

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

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:python字符串str使用.py
@Time:2021/3/21 23:00
@Motto:不积跬步无以至千里	,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

"""

str1 = "hello world"
print("%s 字符串总长度:%d" % (str1,len(str1))) # len()获取字符串长度

#方法一:
for i in str1:
    print(i,end="-")  # print 函数默认换行,强制将换行符改为 '-'	,可以改为任意字符

print("\n") # "\n" 表示换行
print("*"*20)

#方法二:
for i in range(0,len(str1)):
    print(str1[i],end=' ') # 每个字符以空格隔开

print("\n") # "\n" 表示换行
print("*"*20)

#方法三:
a = 0
while a < len(str1):
    print("str[%d] = %s " % (a,str1[a]))
    a += 1
print("程序结束,退出程序")


'''
输出结果:

hello world 字符串总长度:11
h-e-l-l-o- -w-o-r-l-d-

********************
h e l l o   w o r l d

********************
str[0] = h
str[1] = e
str[2] = l
str[3] = l
str[4] = o
str[5] =
str[6] = w
str[7] = o
str[8] = r
str[9] = l
str[10] = d
程序结束,退出程序


'''

四.Python 字符串截取

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

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:python字符串str使用.py
@Time:2021/3/21 23:00
@Motto:不积跬步无以至千里,不积小流无以成江海	,程序人生的精彩需要坚持不懈地积累!

"""

str1 = "猿说python"
print(len(str1))            # 内置函数 len() 获取字符串长度
print(str1)               # 打印字符串
print(str1[2])             # 获取字符串中的第二个字符
print(str1[0:2])            # 截取字符串索引值为0~1的字符,不包括索引值为2的字符
print(str1[2:5])            # 截取字符串索引值为2~4的字符,不包括索引值为5的字符
print(str1[2:-1])            # 截取字符串重索引值为2开始直到字符串结尾的前一个	,-1的索引值表示最后一个
print(str1[2:len(str1)])            # 截取字符串索引值2~8,最后一个字符的索引值为7,所以刚刚好能截取到字符串末尾

# 截取在列表中索引值为0-4的数据	,冒号前面不设置参数,默认重0开始,注意截取并不包括4
print(str1[:4])
# 截取在列表中索引值为2-末尾的数据	,冒号后面不设置参数,默认截取到最后一位数据,注意截取包括最后一位
print(str1[2:])

print("程序结束	,退出程序")


'''
输出结果:

8
猿说python
p
猿说
pyt
pytho
python
猿说py
python
程序结束,退出程序


'''

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

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

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

str.replace(old, new)

示例代码如下:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:python字符串str使用.py
@Time:2021/3/21 23:00
@Motto:不积跬步无以至千里	,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

"""

str1 = "hello world"
str1 = str1.replace("hello","猿说PYTHON")
print(str1)

str1 = "hello world"
str1 = str1.replace("world","python 教程")
print(str1)

'''
输出结果:

猿说PYTHON world
hello python 教程
'''

六.Python 字符串大小写

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

  • upper:把所有字符中的小写字母转换成大写字母

  • lower:把所有字符中的大写字母转换成小写字母

  • capitalize:把第一个字母转化为大写字母 ,其余小写

  • title:把每个单词的第一个字母转化为大写,其余小写

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

关于字符串的函数还有很多 ,由于篇幅有限 ,后面的文章我们继续讲解更多关于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 字符串

本文由博客 - 猿说编程 猿说编程 发布!

文章来源于网络,如有侵权请联系站长QQ61910465删除
本文版权归去快排Seo www.SEOgurublog.com 所有,如有转发请注明来出,竞价开户托管,seo优化请联系QQ▷61910465