首页 > 编程语言 >Python基础之字符串

Python基础之字符串

时间:2022-11-24 10:32:23浏览次数:42  
标签:下标 Python 基础 print 字符串 world mystr hello

一、认识字符串

字符串是 Python 中最常⽤的数据类型。我们⼀般使⽤引号来创建字符串。创建字符串很简单,只要为变量分配⼀个值即可。

1、字符串特征

⼀对引号字符串
name1 = 'Tom'
name2 = "Rose"
三引号字符串
name3 = ''' Tom '''
name4 = """ Rose """
a = ''' i am Tom,
nice to meet you! '''
b = """ i am Rose,
nice to meet you! """
注意:三引号形式的字符串⽀持换⾏。

如果创建⼀个字符串 I'm Tom ?
c = "I'm Tom"
d = 'I\'m Tom'

2、字符串输出

print('hello world')
name = 'Tom'
print('我的名字是%s' % name)
print(f'我的名字是{name}')

3、字符串输⼊

在Python中,使⽤ input() 接收⽤户输⼊。
name = input('请输⼊您的名字:')
print(f'您输⼊的名字是{name}')
print(type(name))
password = input('请输⼊您的密码:')
print(f'您输⼊的密码是{password}')
print(type(password))

二、下标

“下标” ⼜叫 “索引” ,就是编号。⽐如⽕⻋座位号,座位号的作⽤:按照编号快速找到对应的座位。同理,下标的作⽤即是通过下标快速找到对应的数据。

1、示例
name = "abcdef"
print(name[1])
print(name[0])
print(name[2])
注意:下标从0开始。

三、切片

序列[开始位置下标:结束位置下标:步⻓]
注意
1. 不包含结束位置下标对应的数据, 正负整数均可;
2. 步⻓是选取间隔,正负整数均可,默认步⻓为1。

name = "abcdefg"
print(name[2:5:1]) # cde
print(name[2:5]) # cde
print(name[:5]) # abcde
print(name[1:]) # bcdefg
print(name[:]) # abcdefg
print(name[::2]) # aceg
print(name[:-1]) # abcdef, 负1表示倒数第⼀个数据
print(name[-4:-1]) # def
print(name[::-1]) # gfedcba

四、常⽤操作⽅法

字符串的常⽤操作⽅法有查找、修改和判断三⼤类。

1、查找

所谓字符串查找⽅法即是查找⼦串在字符串中的位置或出现的次数。
1、find():
检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则返回-1。
语法:字符串序列.find(⼦串, 开始位置下标, 结束位置下标)
注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找。

mystr = "hello world and itcast and itheima and Python"
print(mystr.find('and')) # 12
print(mystr.find('and', 15, 30)) # 23
print(mystr.find('ands')) # -1

2、index():
检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则报异常。
语法:字符串序列.index(⼦串, 开始位置下标, 结束位置下标)
注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找。

mystr = "hello world and itcast and itheima and Python"
print(mystr.index('and')) # 12
print(mystr.index('and', 15, 30)) # 23
print(mystr.index('ands')) # 报错

3、count():
返回某个⼦串在字符串中出现的次数
语法:字符串序列.count(⼦串, 开始位置下标, 结束位置下标)
注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找。
mystr = "hello world and itcast and itheima and Python"
print(mystr.count('and')) # 3
print(mystr.count('ands')) # 0
print(mystr.count('and', 0, 20)) # 1

4、从右侧查找
rfind(): 和find()功能相同,但查找⽅向为右侧开始。
rindex():和index()功能相同,但查找⽅向为右侧开始。

2、修改

所谓修改字符串,指的就是通过函数的形式修改字符串中的数据。
1、replace():替换
语法:字符串序列.replace(旧⼦串, 新⼦串, 替换次数)
注意:替换次数如果查出⼦串出现次数,则替换次数为该⼦串出现次数。

mystr = "hello world and itcast and itheima and Python"
# 结果:hello world he itcast he itheima he Python
print(mystr.replace('and', 'he'))
# 结果:hello world he itcast he itheima he Python
print(mystr.replace('and', 'he', 10))
# 结果:hello world and itcast and itheima and Python
print(mystr)
注意:数据按照是否能直接修改分为可变类型和不可变类型两种。字符串类型的数据修改的时候不能改变原有字符串,属于不能直接修改数据的类型即是不可变类型。

2、split():按照指定字符分割字符串。
语法:字符串序列.split(分割字符, num)
注意:num表示的是分割字符出现的次数,即将来返回数据个数为num+1个。
分隔结果是一个列表

mystr = "hello world and itcast and itheima and Python"
# 结果:['hello world ', ' itcast ', ' itheima ', ' Python']
print(mystr.split('and'))
# 结果:['hello world ', ' itcast ', ' itheima and Python']
print(mystr.split('and', 2))
# 结果:['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(mystr.split(' '))
# 结果:['hello', 'world', 'and itcast and itheima and Python']
print(mystr.split(' ', 2))
注意:如果分割字符是原有字符串中的⼦串,分割后则丢失该⼦串。

3、join():
⽤⼀个字符或⼦串合并字符串,即是将多个字符串合并为⼀个新的字符串。
语法:字符或⼦串.join(多字符串组成的序列)

list1 = ['chuan', 'zhi', 'bo', 'ke']
t1 = ('aa', 'b', 'cc', 'ddd')
# 结果:chuan_zhi_bo_ke
print('_'.join(list1))
# 结果:aa...b...cc...ddd
print('...'.join(t1))

4、capitalize():将字符串第⼀个字符转换成⼤写。
mystr = "hello world and itcast and itheima and Python"
# 结果:Hello world and itcast and itheima and python
print(mystr.capitalize())
注意:capitalize()函数转换后,只字符串第⼀个字符⼤写,其他的字符全都⼩写。

5、title():将字符串每个单词⾸字⺟转换成⼤写。
mystr = "hello world and itcast and itheima and Python"
# 结果:Hello World And Itcast And Itheima And Python
print(mystr.title())

6、lower():将字符串中⼤写转⼩写。
mystr = "hello world and itcast and itheima and Python"
# 结果:hello world and itcast and itheima and python
print(mystr.lower())

7、upper():将字符串中⼩写转⼤写。
mystr = "hello world and itcast and itheima and Python"
# 结果:HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON
print(mystr.upper())

8、lstrip():删除字符串左侧空⽩字符。 #print(mystr.lstrip())
9、rstrip():删除字符串右侧空⽩字符。 #print(mystr.rstrip())
10、strip():删除字符串两侧空⽩字符。 #print(mystr.strip())

11、ljust():返回⼀个原字符串左对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串。
语法:字符串序列.ljust(⻓度, 填充字符)
print(mystr.ljust(10,'.'))
12、rjust():返回⼀个原字符串右对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串,语法和ljust()相同。
13、center():返回⼀个原字符串居中对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串,语法和ljust()相同。

3、判断

所谓判断即是判断真假,返回的结果是布尔型数据类型:True 或 False

1、startswith():
检查字符串是否是以指定⼦串开头,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。
语法:字符串序列.startswith(⼦串, 开始位置下标, 结束位置下标)
mystr = "hello world and itcast and itheima and Python "
# 结果:True
print(mystr.startswith('hello'))
# 结果False
print(mystr.startswith('hello', 5, 20))

2、endswith():
检查字符串是否是以指定⼦串结尾,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。
语法:字符串序列.endswith(⼦串, 开始位置下标, 结束位置下标)
mystr = "hello world and itcast and itheima and Python"
# 结果:True
print(mystr.endswith('Python'))
# 结果:False
print(mystr.endswith('python'))
# 结果:False
print(mystr.endswith('Python', 2, 20))

3、isalpha():
如果字符串⾄少有⼀个字符并且所有字符都是字⺟则返回 True, 否则返回 False。
mystr1 = 'hello'
mystr2 = 'hello12345'
# 结果:True
print(mystr1.isalpha())
# 结果:False
print(mystr2.isalpha())

4、isdigit():
如果字符串只包含数字则返回 True 否则返回 False。
mystr1 = 'aaa12345'
mystr2 = '12345'
# 结果: False
print(mystr1.isdigit())
# 结果:False
print(mystr2.isdigit())

5、isalnum():
如果字符串⾄少有⼀个字符并且所有字符都是字⺟或数字则返 回 True,否则返回False。
mystr1 = 'aaa12345'
mystr2 = '12345-'
# 结果:True
print(mystr1.isalnum())
# 结果:False
print(mystr2.isalnum())

6、isspace():
如果字符串中只包含空⽩,则返回 True,否则返回 False。
mystr1 = '1 2 3 4 5'
mystr2 = ' '
# 结果:False
print(mystr1.isspace())
# 结果:True
print(mystr2.isspace())

标签:下标,Python,基础,print,字符串,world,mystr,hello
From: https://blog.51cto.com/u_13236892/5882603

相关文章

  • Python的特点有哪些?
    python的五个特点:1、简单易学python是一种代表简单主义思想的语言,阅读一个良好的python程序就感觉像是在读英语段落一样,尽管这个英语段的语法要求非常严格。python最大的优......
  • python主要可以做什么
    python主要可以做Web和Internet开发、科学计算和统计、桌面界面开发、软件开发、后端开发等领域的工作。Python是一种解释型脚本语言。Python可以应用于众多领域,如:数据分......
  • python 爬虫自学要多久
    一周或者一个月。如果完全靠自己自学,又是从零基础开始学习Python的情况下,按照每个人的学习和理解能力的不同,我认为大致上需要半年到一年半左右的时间。当然了,Python学习起来......
  • 算法基础:单链表图解及模板总结
    ⭐写在前面的话:本系列文章旨在复习算法刷题中常用的基础算法与数据结构,配以详细的图例解释,总结相应的代码模板,同时结合例题以达到最佳的学习效果。本专栏面向算法零基础但有......
  • Linux - 《基础环境检查帮助手册》
       检查【操作系统】lsb_release-acat/etc/issuecat/etc/redhat-release 检查【主机名】#>>>>centos6.x<<<<#查看主机名hostnamecat/etc/sys......
  • 第2-4-3章 规则引擎Drools基础语法-业务规则管理系统-组件化-中台
    目录4.Drools基础语法4.1规则文件构成4.2规则体语法结构4.3注释4.4Pattern模式匹配4.5比较操作符4.5.1语法4.5.2操作步骤4.6执行指定规则4.7关键字4.8Drools内......
  • 处理器基础知识
    一,什么是处理器二,指令集基础什么是ISAISA功能三,CPU设计与实现整数范围时钟频率指令周期(Instructioncycle)指令流水线(Instructionpipeline)指令并行(Instru......
  • Python11-eg
    实例01(创建SQLite数据库文件)1importsqlite32#连接到SQLite数据库3#数据库文件是mrsoft.db,如果文件不存在,会自动再当前目录创建4conn=sqlite3.connect('mrsoft.db'......
  • Linux基础命令、Linux简单服务器搭建步骤
    1.三个基本命令lspwdcdpwd表示现在命令所处目录ls列出当前目录下所有文件-L垂直列出文件夹详情-a显示隐藏文件-h显示文件大小cd打开某个文件夹cd-在上个目录......
  • Python爬取某个网站的图片
    最近需要将某个网站的图片爬取下来,想尽了很多办法,后来使用python的爬虫技术将其爬取出来importrequestsdefdownload_save_img(img_url,file_name):headers={......