建站需要注意哪些,sem优化是什么,漳州模板网站建设,中国新闻社是央企吗一、Python的字符串简介 Python中的字符串是一种计算机程序中常用的数据类型【可将字符串看作是一个由字母、数字、符号组成的序列容器】#xff0c;字符串可以用来表示文本数据。 通常使用一对英文的单引号#xff08;#xff09;或者双引号#xff08;#xff09;…一、Python的字符串简介 Python中的字符串是一种计算机程序中常用的数据类型【可将字符串看作是一个由字母、数字、符号组成的序列容器】字符串可以用来表示文本数据。 通常使用一对英文的单引号或者双引号包含起来其中Python还支持使用一对三引号来表示多行字符串如下图所示
# 方式1
str1 你好 牛奶咖啡
print(str1)# 方式2
str2 你好 测试字符串
print(str2)# 方式3
str3 这是一个Python课程
讲解的字符串类型
可以多行表示
print(str3)
str4 1. 购买道具2. 攻击3. 逃跑4. 退出print(str4) 二、Python字符串的操作
2.1、Python转义符 Python转义符 序号Python转义符说明1\表示单引号【】2\表示双引号【】3\\表示反斜杠【\】4\n表示换行5\t表示水平制表符【代表着四个空格也就是一个tab它的主要作用是对齐表格数据的各列使得输出更易于阅读和理解】6\r表示将光标的位置回退到本行的开头位置
# 转义符 \
# 1、将某些普通符号给于特殊的功能
# 2、将一些特殊功能的符号普通化
import time# \:表示双引号【】
str1 Hes my teacher!\测试\
print(str1)# \:表示单引号【】
str2 He\s my teacher!
print(str2)# \\表示反斜杠【\】
str3 F:\\LearnProjects\\Python\\getRandom.py
print(str3)# \n:表示换行
str4 \nI am CoffeeMilk\nmy age is 26\n
print(str4)# \t:表示水平制表符【代表着四个空格也就是一个tab它的主要作用是对齐表格数据的各列使得输出更易于阅读和理解】
str5 序号\t姓名\t年龄\t性别
print(str5)
str6 132\t张三\t\t23\t男
133\t杨新宇\t24\t男
134\t魏思思\t23\t女print(str6)# \r 表示将光标的位置回退到本行的开头位置
str7 \r字符串\r测试回车符
print(str7)# \r实现一个进度效果
for i in range(1,11):str71 \r # * if{i*10}%time.sleep(0.26)print(str71, sep, end)
print(\n打印结束)# \r实现删除效果
str72 等闲识得东风面万紫千红总是春。
strLen len(str72)
for i in range(strLen):print(\r str72[:strLen-1-i] |, end)time.sleep(0.36)# \r实现本地时间实时显示
while True:curTime time.strftime(%Y-%m-%d %H:%M:%S, time.localtime())time.sleep(0.1)print(f\r当前时间为:{curTime}, end) 2.2、Python字符串格式化和输入输出 Python字符串的格式化和输入输出 序号Python字符串格式化和输入输出说明1 字符串格式化【占位符】 %占位符 、str.format() %s字符串类型 %d十进制整数类型 %f浮点数类型 %x十六进制整数类型 %o八进制整数类型 %r原始字符串类型带引号 2字符串格式化【f-string变量插入】是Python3.6新引入的一种字符串格式化方法主要目的是使格式化字符串的操作更加简便。f-string在形式上是以 f 或 F 修饰符引领的字符串fxxx 或 Fxxx以大括号 {} 标明被替换的字段f-string在本质上并不是字符串常量而是一个在运行时运算求值的表达式3字符串输入【input】使用input()函数来获取输入内容4字符串输出【print】使用print()函数来展示输出内容
name 牛奶咖啡
age 26
height 180# 方法1%占位符
print(我的名字是%s年龄%d身高是%dcm % (name, age, height))# 方法2str.format()
print(我的名字是{}年龄{}身高是{}cm.format(name, age, height))# 方法3f-string
print(f我的名字是{name}年龄{age}身高是{height}cm)
运行结果如下 # 使用input()函数来获取输入内容
nameinput(请输入你的名字)
print(name)
ageinput(请输入你的年龄)
print(age)
print(f姓名{name} 年龄{age})# 输入的内容转为数字 int(需转换的值)
num1 input(请输入数字1)
num2 input(请输入数字2)
resultnum1num2
print(f{num1}类型是{type(num1)} {num2}类型是{type(num2)} {num1}{num2}{result})
num1_Int int(num1)
num2_Int int(num2)
result2 num1_Int num2_Int
print(f{num1_Int}类型是{type(num1_Int)} {num2_Int}类型是{type(num2_Int)} {num1_Int}{num2_Int}{result2})# 输出的时候可以指定内容分割符号
x 1000
print(100, 你好, x)
print(100, 你好, x, sep_)# 输出的时候可以指定是否换行(不换行)
print(100, 你好, x, end\t)
print(100, 你好, x, end\t)
print(100, 你好, x, end\t) 运行结果如下 2.3、Python字符串序列操作 字符串属于序列类型序列是指一块可以存放多个值的连续内存空间且这些值按照一定顺序排列可以通过每个值所在的位置编号访问。 Python字符串的序列操作 序号Python字符串的序列操作名称说明1索引2切片3长度len()4判断in5拼接、* str 你好欢迎来到Pyhton世界# 1、索引操作字符串[索引]查询字符
print(str[1])
print(str[-1])
print(str[6]\n)# 2、切片操作字符串[开始索引:结束索引] 顾头不顾尾
print(str[3:4])
print(str[3:5])
print(str[3:-1])
print(str[3:])
print(str[:3])
print(获取所有字符串内容, str[:])
print(负索引切片, str[-8:])
print(从左向右切, str[0::1])
print(从左向右切, str[4:16:2])
print(从右向左切翻转, str[9::-1])
print(从右向左切完全翻转, str[::-1], \n)# 3、字符串拼接
name 咖啡
age 26
result 我的名字是name 我的年龄是age
print(result)
result2 f我的名字是{name} 我的年龄是{age}\n
print(result2)# 4、字符串的乘法
print(***)
print(**10)
print(欢迎*3, \n)# 5、字符串的长度
str12 Hello world
print(f【{str12}】字符串的长度是{len(str12)})
str13 你好中国
print(f【{str13}】字符串的长度是{len(str13)}\n)# 6、针对容器类型in 判断判断某个内容是否存在
str14 你好世界
print(f【世】属于【{str14}】, 世 in str14)
print(f【你好】属于【{str14}】, 你好 in str14)
print(f【测试】属于【{str14}】, 测试 in str14)
运行结果如下 三、Python字符串的内置方法 Python字符串内置方法 序号Python字符串的内置方法名称内置方法说明示例结果1upper() 全部大写 Hello World.upper()HELLO WORLD2lower() 全部小写 Hello World.lower()hello world3startswith() 是否以指定内容开头 张三.startswith(张)True4endswith()是否以指定内容结尾a.mp3.endswith(.jpg)False5isdigit() 是否是数字字符串 36.isdigit()True6strip()去除字符串首尾的空格或换行符号 hi,Milk \n.strip() hi,Milk7join()将多个字符串连接在一起 ;.join([哈尔滨, 深圳, 内蒙古]) 哈尔滨;深圳;内蒙古8split()将字符串按照某个字符分割默认按照空格分割 哈尔滨-深圳-内蒙古.split(-) [哈尔滨, 深圳, 内蒙古]9find() 搜索指定字符串存在在返回搜索内容所在位置的开始索引没有找到则返回-1 你好张三中国欢迎你.find(张三) 310index()搜索指定字符串存在在返回搜索内容所在位置的开始索引没有找到则直接报错 你好张三中国欢迎你.index(张三) 311count() 统计指定字符串出现的次数 张三 李四 王五 赵六 张三.count(张三) 212replace()将需要替换的内容替换为新的内容 I am coffeemilk.replace(coffee, tea) I am teamilk
# 数据类型的内置方法数据类型对象.方法名(参数)# 1、字符串全部转为大写或小写方法
str1 Hello World
str1_upper str1.upper()
print(f{str1} 全部转为大写是{str1_upper})
print(f{str1} 全部转为小写是{str1.lower()}\n)# 2、startswith_表示字符串是否以什么开头 endwith_表示字符串是否以什么结尾 方法
str2 apple banana peach orange
startStyle apple
startStyle2 ap
startStyle3 bana
print(f【{str2}】字符串是否以【{startStyle}】开头{str2.startswith(startStyle)})
print(f【{str2}】字符串是否以【{startStyle2}】开头{str2.startswith(startStyle2)})
print(f【{str2}】字符串是否以【{startStyle3}】开头{str2.startswith(startStyle3)}\n)
str21 张三
startStyle22 张
startStyle23 杨
print(f【{str21}】字符串是否以【{startStyle22}】开头{str21.startswith(startStyle22)})
print(f【{str21}】字符串是否以【{startStyle23}】开头{str21.startswith(startStyle23)}\n)url1 http://www.baidu.com/test/image/a.jpg
endStyle .jpg
endStyle2 .mp3
print(f【{url1}】地址内容是否以{endStyle}格式结尾{url1.endswith(endStyle)})
print(f【{url1}】地址内容是否以{endStyle2}格式结尾{url1.endswith(endStyle2)})
运行结果如下 # 1、isdigit:判断输入的内容是否是数字字符串
str input(请输入数字)
if str.isdigit():print(f{str} 是数字)num int(str)*2print(f{str}的2倍是{num})
else:print(f当前输入的{str}不是数字请检查后重试)# 2、strip:去除字符串首尾的空格或换行符
user input(请输入用户名)
print(f【{user}】字符串的长度是{len(user)})
user user.strip()
print(f剔除【{user}】字符串前后的空格或换行符后的长度是{len(user)})
运行结果如下 # 1、split字符串分割
citys 北京 上海 广州 重庆 哈尔滨 深圳 内蒙古
result citys.split( )
print(f【{citys}】字符串共包含的城市为{result}\n)# 2、join拼接内容
result2 ;.join(result)
print(f【{result}】内容拼接后的结果是{result2}\n)# 3、find查找内容存在字符串的开始索引没有找到返回-1
str 你好张三中国欢迎你
needFindStr 张三
result_find str.find(needFindStr)
print(f【{str}】字符串中查找【{needFindStr}】结果是{result_find})
needFindStr2 李四
print(f【{str}】字符串中查找【{needFindStr2}】结果是{str.find(needFindStr2)})# 4、index查找内容是否存在存在则返回对应的开始索引位置查找不到则直接报错
reslult_index str.index(needFindStr)
print(f【{str}】字符串中查找【{needFindStr}】结果是{reslult_index}\n)
# print(f【{str}】字符串中查找【{needFindStr2}】结果是{str.index(needFindStr2)})# 5、count计数
names张三 李四 王五 李四 赵六 张三
find_str张三
print(f【{names} 】字符串中的【{find_str}】共出现了【{names.count(find_str)}】次)# 6、replace替换
info I am coffeemilk
old coffee
new tea
result_info info.replace(coffee,tea)
# info info.replace(coffee,tea)
print(info)
print(f【{info}】字符串替换【{old}】为【{new}】后是{result_info})
# print(f【{info}】替换【{old}】为【{new}】后是{info})
运行结果如下
文章转载自: http://www.morning.tgbx.cn.gov.cn.tgbx.cn http://www.morning.fgrcd.cn.gov.cn.fgrcd.cn http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.drwpn.cn.gov.cn.drwpn.cn http://www.morning.bgpb.cn.gov.cn.bgpb.cn http://www.morning.rnqrl.cn.gov.cn.rnqrl.cn http://www.morning.horihe.com.gov.cn.horihe.com http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.kmwsz.cn.gov.cn.kmwsz.cn http://www.morning.pltbd.cn.gov.cn.pltbd.cn http://www.morning.rsqpc.cn.gov.cn.rsqpc.cn http://www.morning.mjgxl.cn.gov.cn.mjgxl.cn http://www.morning.kqlrl.cn.gov.cn.kqlrl.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.wjplr.cn.gov.cn.wjplr.cn http://www.morning.krdxz.cn.gov.cn.krdxz.cn http://www.morning.wqkfm.cn.gov.cn.wqkfm.cn http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.mfltz.cn.gov.cn.mfltz.cn http://www.morning.sbqrm.cn.gov.cn.sbqrm.cn http://www.morning.gfkb.cn.gov.cn.gfkb.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.mldrd.cn.gov.cn.mldrd.cn http://www.morning.wdrxh.cn.gov.cn.wdrxh.cn http://www.morning.dpjtn.cn.gov.cn.dpjtn.cn http://www.morning.zcckq.cn.gov.cn.zcckq.cn http://www.morning.tfwg.cn.gov.cn.tfwg.cn http://www.morning.bcnsl.cn.gov.cn.bcnsl.cn http://www.morning.mqwnp.cn.gov.cn.mqwnp.cn http://www.morning.rfbpq.cn.gov.cn.rfbpq.cn http://www.morning.mlpch.cn.gov.cn.mlpch.cn http://www.morning.hrtfz.cn.gov.cn.hrtfz.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.nba1on1.com.gov.cn.nba1on1.com http://www.morning.gczzm.cn.gov.cn.gczzm.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.lmxzw.cn.gov.cn.lmxzw.cn http://www.morning.cbndj.cn.gov.cn.cbndj.cn http://www.morning.wwnb.cn.gov.cn.wwnb.cn http://www.morning.wbrf.cn.gov.cn.wbrf.cn http://www.morning.cgthq.cn.gov.cn.cgthq.cn http://www.morning.qfmcm.cn.gov.cn.qfmcm.cn http://www.morning.gtmgl.cn.gov.cn.gtmgl.cn http://www.morning.jnbsx.cn.gov.cn.jnbsx.cn http://www.morning.bpmz.cn.gov.cn.bpmz.cn http://www.morning.kldtf.cn.gov.cn.kldtf.cn http://www.morning.rcttz.cn.gov.cn.rcttz.cn http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn http://www.morning.ydhck.cn.gov.cn.ydhck.cn http://www.morning.qnxtz.cn.gov.cn.qnxtz.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn http://www.morning.nwcgj.cn.gov.cn.nwcgj.cn http://www.morning.xflzm.cn.gov.cn.xflzm.cn http://www.morning.ksbmx.cn.gov.cn.ksbmx.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.ftwlay.cn.gov.cn.ftwlay.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.ctqlq.cn.gov.cn.ctqlq.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.cgtfl.cn.gov.cn.cgtfl.cn http://www.morning.hbjqn.cn.gov.cn.hbjqn.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.prfrb.cn.gov.cn.prfrb.cn http://www.morning.ksqzd.cn.gov.cn.ksqzd.cn http://www.morning.mxlwl.cn.gov.cn.mxlwl.cn http://www.morning.yxdrf.cn.gov.cn.yxdrf.cn http://www.morning.nccqs.cn.gov.cn.nccqs.cn http://www.morning.gghhmi.cn.gov.cn.gghhmi.cn http://www.morning.bxfy.cn.gov.cn.bxfy.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.dblgm.cn.gov.cn.dblgm.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.mwcqz.cn.gov.cn.mwcqz.cn http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.xswrb.cn.gov.cn.xswrb.cn http://www.morning.knscf.cn.gov.cn.knscf.cn http://www.morning.dshkp.cn.gov.cn.dshkp.cn http://www.morning.lwtld.cn.gov.cn.lwtld.cn http://www.morning.lrwsk.cn.gov.cn.lrwsk.cn