Python字符串及其他类型

本文详细介绍了Python的变量命名规则、数据类型,特别是字符串的操作,包括拼接、替换、大小写转换等。还讨论了列表的增、删、查、改操作,以及字典的添加、删除、修改和查询。此外,涵盖了布尔类型和集合、元组的基本概念。
摘要由CSDN通过智能技术生成

目录

一、Python变量命名规则

“数字字母下划线,数字不能打开头”

 #驼峰命名法
 HelloWorld = 'world'
 #'_'连接
 book_page_num = 123

二、if else判断语句写法

if 7>3:
    print(123)
    if 4<5:
        print(777)
elif 7<3:
    print(456)
else:
    print(789)

三、Python数据类型

Numbers(数字) int float
String(字符串) str
List(列表) list
Tuple(元组)tuple
Dictionary(字典) dict
Set(集合) set
Boolean(布尔) bool
Python 的八个标准数据类型中:

不可变数据类型(5 个):int、float、str、bool、tuple;
可变数据类型(3 个):list、set、dict;

# int
print(23332)
# float
print(1222.222)
# str
str1 = 'hello world'
# list(可变)
print([1,2,3,4])
lists = [1,2,3]
print(id(lists))
lists.append(4)
print(id(lists))
# tuple
print((1,2,3,4))
# set(可变)
print({
   1,2,3,4})
# dict(可变)
print({
   'hello':'world',123:'world'})
# bool
# True False

四、字符串的各种操作

1.字符串拼接

可以直接使用“+”号

# 字符串拼接+
str1 = 'hello world'
str2 = 'Hi'
str3 = 'hello world' + 'Hi'
print(str1+str2,str3)

使用id()方法可以查看数据的真实内存地址

print(id(str1))
print(id(str1+str2))
print(id(str3))
hello worldHi hello worldHi
2729388342576
2729389418672
2729389416368

Process finished with exit code 0

2.字符串替换

使用replace()方法,replace(‘要替换的内容’,‘替换后的内容’)

demo1 = '今天天气不错!123456789'
demo2 = demo1.replace('不错','很坏')
print(demo1)
print(demo2)
今天天气不错!123456789
今天天气很坏!123456789

Process finished with exit code 0

3.字符串中字母大小写转换

upper() 全部大写
lower() 全部小写
swapcase() 大小写互转
title() 只有首字母大写(驼峰)

demo1 = 'hello WORLD'
print(demo1.upper())
print(demo1.lower())
print(demo1.swapcase())
print(demo1.title())
HELLO WORLD
hello world
HELLO world
Hello World

Process finished with exit code 0

4.去除字符串左右两遍的 空字符串和指定字符

strip()方法,
lstrip()去除左边
rstrip()去除右边

注: 只能删除最左边和最右边的空白字符和指定字符,
不能删除字符串中间的字符,字符串不可变

# 去左右空白字符及指定字符
demo1 = '    hello    world     123aaaaa'
demo2 = '    hello    world     123aaaaa   '
# 去两边
print(demo1.strip())
# 去左
print(demo1.lstrip())
# 去右
print(demo1.rstrip())
# 去右边s
print(demo1.rstrip('a'))
#当字符串最右边有空白字符时,不能直接清楚指定字符
print(demo2.rstrip('a'))
hello    world     123aaaaa
hello    world     123aaaaa
    hello    world     123aaaaa
    hello    world     123
    hello    world     123aaaaa   

Process finished with exit code 0

5.字符串拆分

split()方法
split(‘_’),指定用’_'拆分,拆分后‘_’不保留
拆分后的每一段存进一个列表

#字符串拆分
demo1 = 'hello world_你好啊_啦啦啦'
list1 = demo1.split('_')
#拆分后的内容以列表存放
print(list1)
['hello world', '你好啊', '啦啦啦']

Process finished with exit code 0

6.列表连成字符串

‘ ’.join()方法 ‘ ’内是指定用来连接的内容

#列表连成字符串
demo1 = 'hello world_你好啊_啦啦啦'
list1 = demo1.split('_')
demo2 = '_'.join(list1)
print(list1)
print(demo2)

['hello world', '你好啊', '啦啦啦']
hello world_你好啊_啦啦啦

Process finished with exit code 0

7.字符串查询

使用字符串下标查询,用

for i in range str1:

遍历字符串中每一个字符
也可以直接像输出列表内容一样,str1[2]取字符串中第3个元素
“计数从0开始,数字+1=要取的元素位置”
倒数第一个是从[-1]开始,[-4]是倒数第四个字符

# 字符串查询
str1 = 'hello world'
for i in str1:
    print(i)
print('******')
print(str1[-4])
h
e
l
l
o
 
w
o
r
l
d
******
o

Process finished with exit code 0


find()方法,查找字符串中指定元素的下标,返回值为数字
index()方法,同样是查找字符串中指定元素的下标,返回值为数字
不同的是:find找不到返回-1 index则是报错

str1 = 'hello world 你好_世界123'
# 字符串查询(取你好三个字)
# find找不到返回-1 index则是报错
# 方法一
a = str1.find('我')
print(a)
b = str1.index('我')
print(b)
Traceback (most recent call last):
  File "D:/PycharmProjects/Python学习/1/demo.py", line 64, in <module>
    b = str1.index('我')
ValueError: substring not found
-1

Process finished with exit code 1

n=14
str1[n+1:n+4] 取字符串中第n+1个到n+4个元素

str1 = 'hello world 你好_世界123'
# 字符串查询(取你好三个字)
# find找不到返回-1 index则是报错
# 方法一
num = str1.find('_')
print(num)
print(str1[num+1:num+4])
# 方法二
num = str1.index('d')
print(str1[num+1:num+4])
14
世界1
 你好

Process finished with exit code 0

8.求字符串长度

len()方法,返回值为int数字

# 求字符串长度
str1 = 'hello world'
print(len(str1))

for i in range(len(str1)):
    print(str1[i])
11
h
e
l
l
o
 
w
o
r
l
d

Process finished with exit code 0

for i in range(2,10,2)的含义:从2开始,遍历到10,每次自增2

for i in range(2,10,2):
    print(i)
2
4
6
8

Process finished with exit code 0

9.字符串倒序

str[ : :-1] 每次自增-1,即为倒序

# 字符串倒序
str1 = 'he
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值