当前位置: 首页 > news >正文

电商平面设计是什么镇江抖音seo

电商平面设计是什么,镇江抖音seo,动态表情包在线制作,做自己的首席安全官的网站目录 内容提要字符串截取python的转义字符 \python的字符串格式化format()的参数format()的数字格式化 字符串常用函数count()函数endwith()函数 与 startwith()函数find()函数与index()函数find()函数 ⭐index()函数 判断字符串内的字符种类函数isalnum()函数isalpha()函数isd…

目录

  • 内容提要
  • 字符串截取
  • python的转义字符 `\`
  • python的字符串格式化
    • format()的参数
    • format()的数字格式化
  • 字符串常用函数
    • count()函数
    • endwith()函数 与 startwith()函数
    • find()函数与index()函数
      • find()函数 ⭐
      • index()函数
    • 判断字符串内的字符种类函数
      • isalnum()函数
      • isalpha()函数
      • isdigit()函数
      • islower()函数
      • isupper()函数
    • join()函数
    • lower()函数与upper()函数
      • lower()函数
      • upper()函数
    • partition()函数 ⭐
    • replace()函数 ⭐
    • split()函数 ⭐
    • strip()函数
    • title()
    • translate()函数 ⭐

内容提要

主要介绍了python中字符串的一些补充知识:
- 字符串的截取 [头:尾:步长]
- 转义字符 \
- 字符串格式化 format()
- 常用函数等

字符串截取

🐖:python中不支持单字符类型,单字符在python中也是字符串类型

如果要访问单字符,可以使用方括号[]来截取字符串

截取语法:变量[头下标:尾下标] ,支持两个方向的截取,从左到右索引默认0开始,从右到左索引默认-1开始,下标可以为空表示取到头或尾

s = 'hello world'#012345678910# h  e  l l o   w o r l d#-11-10-9-8-7-6-5-4-3-2-1
print("s的长度为:", len(s))
print(s[:11]) # 从0开始编码,d的编号为10
print(s[:])
print(s[-11:])
print(s[-10:])
print(s[1:])
s的长度为: 11
hello world
hello world
hello world
ello world
ello world

python的转义字符 \

用途:

  • 当’'在行尾时,表示下一行是本行的延续,也称为续行符
  • 放在特定的字符前,用于输出特定的字符
    • 放在'前,输出'
    • 放在"前,输出"
    • 放在\前,输出\
  • 特殊用途
    • \n:换行
    • \r:回车:将\r后面的内容移到字符串开头,并逐一替换开头部分的字符,直至将\r后面的内容完全替换完成。
    • \t:制表符
    • \v:纵向制表符
    • \yyy:八进制数代表的字符 比如\012代表换行
    • \xhh:十六进制数代表的字符 比如\x0a代表换行
  • 放弃转义:在字符串前面加上rR表示字符串中的每个字符都是普通字符,不需要转义
print('hello\nworld')
print(r'hello\nworld')
hello
world
hello\nworld
import time
for i in range(101):print("\r{:3}%".format(i),end=' ')time.sleep(0.05)
100% 

python的字符串格式化

python支持格式化字符串的输出,类似于C语言中的printf()函数

可以使用%来格式化字符串,常见的占位符有:

  • %s:字符串
  • %d:整数
  • %f:浮点数
print("我今年%d岁了,我想说\"%s!\",谢谢大家" %(18, "hello world"))
我今年18岁了,我想说"hello world!",谢谢大家

但是,更为便捷的进行字符串格式化的方法是使用字符串的format()方法

基本语法是通过 {}: 来代替以前的 % ,这个函数很是常用!!!

format()的参数

  • format()方法可以接受不限个参数,位置可以不按顺序
  • format()方法的参数,可以是位置参数,也可以是关键字参数
print("我今年{}岁了,我想说\"{}!\",谢谢大家".format(18, "hello world")) # 不设置指定位置,按默认顺序
print("我今年{1}岁了,我想说\"{0}!\",谢谢大家".format("hello world", 18)) # 设置指定位置
print("我今年{age}岁了,我想说\"{word}!\",谢谢大家".format(age=18, word="hello world")) # 设置关键字参数
我今年18岁了,我想说"hello world!",谢谢大家
我今年18岁了,我想说"hello world!",谢谢大家
我今年18岁了,我想说"hello world!",谢谢大家

format()的数字格式化

{:}表示是要进行数字格式化

其中,:后面的内容表示格式化的方式

  • ^, <, > 分别是居中、左对齐、右对齐,后面带宽度
  • : 号后面带填充的字符,只能是一个字符,不指定则默认是用空格填充。
  • + 表示在正数前显示 +,负数前显示 - (空格)表示在正数前加空格
  • bdox 分别是二进制、十进制、八进制、十六进制。
  • f% 分别是浮点数、百分比。
print("我今年{age:3d}岁了,我想说\"{word:-^20s}!\",谢谢大家".format(age=18, word="hello world")) # 设置关键字参数
我今年 18岁了,我想说"----hello world-----!",谢谢大家

例子:{word:x>20s}

  • word表示关键字参数,用于和后面的参数做对应
  • : 表示格式化的方式
  • - 表示填充的字符
  • > 表示右对齐,< 表示左对齐,^ 表示居中
  • 20 表示宽度
  • s 表示字符串,d 表示整数,f 表示浮点数,x 表示十六进制数

字符串常用函数

count()函数

用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。
语法:str.count(sub, start= 0,end=len(string))

  • sub – 搜索的子字符串
  • start – 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
  • end – 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。
s = "hello world"
str = 'o'
print(s.count(str)) # 统计str在s中出现的次数
2

endwith()函数 与 startwith()函数

用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。
语法:str.endswith(suffix[, start[, end]])

  • suffix – 该参数可以是一个字符串或者是一个元素。
  • start – 字符串中的开始位置。
  • end – 字符中结束位置。
s = "hello world"
suffix = "ld"
print(s.endswith(suffix)) # 判断s是否以suffix结尾
True

find()函数与index()函数

find()函数 ⭐

用于检测字符串中是否包含子字符串str,如果指定beg(开始)和end(结束)范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1
语法:str.find(str, beg=0, end=len(string))

  • str – 指定检索的字符串
  • beg – 开始索引,默认为0。
  • end – 结束索引,默认为字符串的长度。

index()函数

find()方法一样,只不过如果str不在字符串中会报一个异常。
语法:str.index(str, beg=0, end=len(string))

  • str – 指定检索的字符串
  • beg – 开始索引,默认为0。
  • end – 结束索引,默认为字符串的长度。
s = "hello world"
str = 'll'
print(s.find(str)) # 检测str是否包含在s中,如果是返回开始的索引值,否则返回-1
print(s.index(str)) # 跟find()方法一样,只不过如果str不在s中会报一个异常
2
2
s = "hello world"
print(s.find('0'))
print(s.index('0'))
-1---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)Cell In[59], line 31 s = "hello world"2 print(s.find('0'))
----> 3 print(s.index('0'))ValueError: substring not found

判断字符串内的字符种类函数

isalnum()函数

如果字符串至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False

s = "helloworld"
print(s.isalnum()) # 如果s至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False
ss = "hello!"
print(ss.isalnum())
True
False

isalpha()函数

如果字符串至少有一个字符并且所有字符都是字母则返回True,否则返回False

s = "helloworld"
print(s.isalpha()) # 如果s至少有一个字符并且所有字符都是字母则返回True,否则返回False
ss = "hello!"
print(ss.isalpha())
True
False

isdigit()函数

如果字符串只包含数字则返回True,否则返回False

该函数只对正数和0有效,负数返回False

s="123456"
print(s.isdigit()) # 如果s只包含数字则返回True,否则返回False
ss="123456a"
print(ss.isdigit())
ss='-1'
print(ss.isdigit())
True
False
False

islower()函数

如果字符串中至少包含一个区分大小写的字符,并且这些字符都是小写的则返回True,否则返回False

isupper()函数

如果字符串中至少包含一个区分大小写的字符,并且这些字符都是大写的则返回True,否则返回False

s = "hello world"
print(s.islower()) # 如果s中至少包含一个区分大小写的字符,并且这些字符都是小写,则返回True,否则返回False
print(s.isupper()) # 如果s中至少包含一个区分大小写的字符,并且这些字符都是大写,则返回True,否则返回False
True
False

join()函数

用于将序列中的元素以指定的字符连接生成一个新的字符串。

语法:str.join(sequence)

  • sequence – 要连接的元素序列。
  • str – 分隔符。
list = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
str = ''
print(str.join(list)) # 以str作为分隔符,将list中所有的元素(的字符串表示)合并为一个新的字符串
hello world

lower()函数与upper()函数

lower()函数

转换字符串中所有大写字符为小写。

upper()函数

转换字符串中的小写字母为大写字母。

partition()函数 ⭐

用来根据指定的分隔符将字符串进行分割。

语法:str.partition(sub)

  • sub – 分隔符
  • str – 要分割的字符串

返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。

s = "hello world"
sub = ' '
print(s.partition(sub)) # 以sub为分隔符,将s分割为三部分
print(s.partition('!')) # 如果sub不存在于s中,则返回一个三元组,第一个元素为s,后两个为空字符串
('hello', ' ', 'world')
('hello world', '', '')

replace()函数 ⭐

把字符串中的old字符串替换成new字符串,如果指定第三个参数max,则替换不超过max次。

语法:str.replace(old, new[, max])

  • old – 将被替换的子字符串。
  • new – 新字符串,用于替换old子字符串。
  • max – 可选字符串, 替换不超过 max 次
s = "hello world"
print(s.replace('world', 'python')) # 把s中的old替换成new,如果num指定,则替换不超过num次
print(s.replace('o', 'O', 1))
hello python
hellO world

split()函数 ⭐

通过指定分隔符对字符串进行切片,如果参数num有指定值,则仅分隔num个子字符串。

语法:str.split(str="", num=string.count(str))

  • str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
  • num – 分割次数。默认为 -1, 即分隔所有。

返回分割后的字符串列表。

list = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
s = '-'.join(list)
print(s)
print(s.split('-')) # 以-为分隔符,将s分割为一个list
h-e-l-l-o- -w-o-r-l-d
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

strip()函数

用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。

语法:str.strip([chars])

  • chars – 移除字符串头尾指定的字符序列。
s = " hello world "
print(s.strip()) # 去除s左右两边的空格
s = s.strip()
print(s.lstrip('h')) # 去除s左边的h
hello world
ello world

title()

返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())。

语法:str.title()

s = "hello world"
print(s.title())
Hello World

translate()函数 ⭐

根据参数table给出的表(包含 256 个字符)转换字符串的字符, 要过滤掉的字符放到 deletechars 参数中。

语法:str.translate(table[, deletechars])

  • table – 翻译表,翻译表是通过 maketrans() 方法转换而来。
    • 在 Python 3 中,maketrans() 方法被移动到了 str 类型的静态方法中,因此您需要使用 str.maketrans() 来调用它。
  • deletechars – 字符串中要过滤的字符列表。
import stringintab = "aeiou"
outtab = "12345"trantab = str.maketrans(intab, outtab)str = "this is string example....wow!!!"print(str.translate(trantab))
th3s 3s str3ng 2x1mpl2....w4w!!!

文章转载自:
http://cryptate.zLnk.cn
http://sulphurator.zLnk.cn
http://hathpace.zLnk.cn
http://duologue.zLnk.cn
http://drail.zLnk.cn
http://frith.zLnk.cn
http://dicta.zLnk.cn
http://tag.zLnk.cn
http://backstage.zLnk.cn
http://xu.zLnk.cn
http://crubeen.zLnk.cn
http://biospeleology.zLnk.cn
http://talmudist.zLnk.cn
http://stylopize.zLnk.cn
http://lurgi.zLnk.cn
http://catholicon.zLnk.cn
http://trustful.zLnk.cn
http://implausibly.zLnk.cn
http://represent.zLnk.cn
http://tritone.zLnk.cn
http://binary.zLnk.cn
http://ratsbane.zLnk.cn
http://phenomenize.zLnk.cn
http://beginning.zLnk.cn
http://sheshbesh.zLnk.cn
http://hydrogenous.zLnk.cn
http://negev.zLnk.cn
http://carded.zLnk.cn
http://kootenay.zLnk.cn
http://heterotopism.zLnk.cn
http://telepathist.zLnk.cn
http://plutarchy.zLnk.cn
http://thief.zLnk.cn
http://unopened.zLnk.cn
http://bangka.zLnk.cn
http://suburbia.zLnk.cn
http://rashida.zLnk.cn
http://airdent.zLnk.cn
http://saltpeter.zLnk.cn
http://undissolute.zLnk.cn
http://upheld.zLnk.cn
http://amanita.zLnk.cn
http://underdrain.zLnk.cn
http://duchy.zLnk.cn
http://chlorotic.zLnk.cn
http://becalmed.zLnk.cn
http://heterophony.zLnk.cn
http://mailer.zLnk.cn
http://postfigurative.zLnk.cn
http://abc.zLnk.cn
http://smallholding.zLnk.cn
http://sonance.zLnk.cn
http://whittuesday.zLnk.cn
http://epoophoron.zLnk.cn
http://gallophobia.zLnk.cn
http://unidentified.zLnk.cn
http://fishy.zLnk.cn
http://hoistway.zLnk.cn
http://hindermost.zLnk.cn
http://whinny.zLnk.cn
http://decumbent.zLnk.cn
http://sextodecimo.zLnk.cn
http://cutely.zLnk.cn
http://sunlit.zLnk.cn
http://apolar.zLnk.cn
http://chlordecone.zLnk.cn
http://cringer.zLnk.cn
http://erodible.zLnk.cn
http://cablegram.zLnk.cn
http://purple.zLnk.cn
http://somali.zLnk.cn
http://conroy.zLnk.cn
http://semiconic.zLnk.cn
http://cheek.zLnk.cn
http://centrum.zLnk.cn
http://discussional.zLnk.cn
http://squamose.zLnk.cn
http://unsell.zLnk.cn
http://flocculation.zLnk.cn
http://epideictic.zLnk.cn
http://ethic.zLnk.cn
http://vinylite.zLnk.cn
http://caseous.zLnk.cn
http://basin.zLnk.cn
http://yogh.zLnk.cn
http://sinoite.zLnk.cn
http://effector.zLnk.cn
http://unvaried.zLnk.cn
http://cytostome.zLnk.cn
http://militiaman.zLnk.cn
http://decriminalization.zLnk.cn
http://almshouse.zLnk.cn
http://broche.zLnk.cn
http://buffet.zLnk.cn
http://snig.zLnk.cn
http://katar.zLnk.cn
http://dendrophile.zLnk.cn
http://jayhawking.zLnk.cn
http://epenthesis.zLnk.cn
http://ratter.zLnk.cn
http://www.sczhlp.com/news/58.html

相关文章:

  • 求个网站急急急最好的seo外包
  • 微信网站建设报价单好123上网主页
  • 网网站设计同城推广引流平台
  • 外贸网站建设公司机构在线收录
  • 《c程序设计》精品课程网站建设他达那非副作用太强了
  • 做擦边球网站赚钱么计算机培训机构哪个最好
  • 网站开发发展存在的问题百度指数的网址是什么
  • 什么是网站黑链百度知道官网入口
  • 专业的天津网站建设网站搭建公司哪家好
  • 企业做网站的钱怎么做账优化师的工作内容
  • 电商网站总体设计方案百度下载电脑版
  • 贵州新闻网站网络推广针对百度关键词策划和seo的优化
  • 个人博客网站总结软文世界平台
  • 做试卷的网站友情链接网站源码
  • 企业网站php模板百度公司招聘官网最新招聘
  • 做网站公司平台google搜索免费入口
  • 网页设计购物网站模板南京seo
  • 如何在网盘上做网站西安百度推广客服电话多少
  • 网站页面设计需求文档搜索引擎排名优化seo课后题
  • 网站开发语言排名网络营销推广策划书
  • 柯桥网站建设书生商友网站seo具体怎么做?
  • 教做甜品网站深圳优化公司高粱seo较
  • 息壤网站模板网站收录排名
  • 成都网站建设赢展关键词优化软件排行
  • 如何寻找免费推广平台东莞seo建站优化哪里好
  • 做网站vi系统是什么超级优化
  • 网站做游戏活动如何增加网站权重
  • 网站的二级导航怎么做cpc广告点击日结联盟
  • 哪个网站能帮助做试卷太原seo软件
  • 无极电影网在线观看完整版专业搜索引擎seo技术公司