Python基础语法(一)

一、认识字符串

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

1	a = 'helloworld'
2	b ="abcdefg"
3	print(type(a))
4	print(type(b))

注意:控制台显示结果为 <class 'str'>, 即数据类型为str(字符串)

1.1字符串特征

  • 一对引号字符串
name1='Tom'
name2="Tom"
  • 三对引号字符串
name3='''Tom'''

思考:如果创建一个字符串 I’m Tom?

name="I'm Tom"

1.2 字符串输出

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

字符串输入

在Python中,使用input()接收用户输入

print(input('请输入名字:'))
print(input('请输入密码:'))
  • 输出结果
  • 在这里插入图片描述

二、下标

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

2.1 快速体验

需求:字符串 name = “abcdef”,取到不同下标对应的数据

name='abcdefg'
print(name[1])
print(name[0])
print(name[2])
  • 结果
    在这里插入图片描述
  • 注意:下标从0开始

三、切片

切片是指对操作的对象截取其中一部分的操作。字符串、列表、元组都支持切片操作。

3.1 语法

序列[开始位置下标:结束位置下标:步⻓]
不包含结束位置的下标

3.2 体验

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 负1指最后一个数据依次前推 
print(name[::-1])	#gfedcba 步长为负1倒序排列

四、常用操作方法

字符串的常用操作方法有查找、修改和判断三大类。

4.1 查找

所谓字符串查找方法即是查找子串在字符串中的位置或出现的次数。

  • find():检测某个子串是否包含在这个字符串中,如果在返回这个子串开始的位置下标,否则则返 回-1。
  1. 语法
字符串序列.find(子串, 开始位置下标, 结束位置下标)

2.快速体验

mystr = "hello world and itcast and itheima andPython"
print(mystr.find('and'))	#12
print(mystr.find('and',15,30))	#23
print(mystr.find('ands'))	#-1
  • index():检测某个子串是否包含在这个字符串中,如果在返回这个子串开始的位置下标,否则则报异常。
    1.语法
字符串序列.index(子串, 开始位置下标, 结束位置下标)

2.快速体验

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

  • rfind(): 和find()功能相同,但查找方向为右侧开始。

  • rindex():和index()功能相同,但查找方向为右侧开始。

  • count():返回某个子串在字符串中出现的次数
    1.语法

字符串序列.count(子串, 开始位置下标, 结束位置下标)

2.快速体验

mystr = "hello world and itcast and itheima andPython"
print(mystr.count('and'))	#3
print(mystr.count('ands'))	#0
print(mystr.count('and',0,20))	#1

4.2 修改

所谓修改字符串,指的就是通过函数的形式修改字符串中的数据。

  • replace() 替换
    1.语法
字符串序列.replace(旧子串, 新子串, 替换次数)

2.快速体验

mystr = "hello world and itcast and itheima andPython"
print(mystr.replace('and', 'he'))
# 结 果 :hello world he itcast he itheima hePython
print(mystr.replace('and', 'he', 10))
# 结 果 :hello world he itcast he itheima hePython 
print(mystr)
# 结 果 :hello world and itcast and itheima andPython 
  • split() 分割字符串
    1.语法
字符串序列.split(分割字符, num)

2.快速体验

mystr = "hello world and itcast and itheima andPython"
print(mystr.split('and'))
# 结果:['hello world ', ' itcast ', ' itheima ', ' Python']
print(mystr.split('and', 2))
# 结 果 :['hello world ', ' itcast ', ' itheima andPython']
print(mystr.split(' '))
# 结 果 :['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(mystr.split(' ', 2))
# 结 果 :['hello', 'world', 'and itcast and itheima andPython']
  • join() 用一个字符或子串合并字符串,即是将多个字符串合并为一个新的字符串。合并列表里面的字符串为一个字符串
    1.语法
字符或子串.join(多字符串组成的序列)

2.快速体验

l1 = ['wo', 'shi', 'chao', 'ren']
t1 = ('aa', 'b', 'cc','ddd')
print('_'.join(l1))
# 结果:wo_shi_chao_ren
print('...'.join(t1))
# 结果:aa...b...cc...ddd
  • capitalize():将字符串第一个字符转换成大写。
mystr = "hello world and itcast and itheima andPython"
print(mystr.capitalize())
# 结 果 :Hello world and itcast and itheima andpython

  • title():将字符串每个单词首字⺟转换成大写。
mystr = "hello world and itcast and itheima andPython"
print(mystr.title())
# 结 果 :Hello World And Itcast And Itheima AndPython

  • lower():将字符串中大写转小写。
mystr = "hello world and itcast and itheima andPython"
print(mystr.lower())
# 结 果 :hello world and itcast and itheima andpython
  • upper():将字符串中小写转大写。

mystr = "hello world and itcast and itheima andPython"
print(mystr.upper())
# 结 果 :HELLO WORLD AND ITCAST AND ITHEIMA ANDPYTHON
  • lstrip():删除字符串左侧空白字符。
    在这里插入图片描述
  • rstrip():删除字符串右侧空白字符。
    在这里插入图片描述
  • strip():删除字符串两侧空白字符。
    在这里插入图片描述

4.3 判断

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

  • startswith():检查字符串是否是以指定子串开头,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。
    1.语法
字符串序列.startswith(子串, 开始位置下标, 结束位置下标)

2.快速体验

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

2.快速体验

mystr="Hello World"
print(mystr.endswith(World))
# 结果 True
print(mystr.endswith(world))
# 结果 False
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值