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

做企业展示版网站贵吗南宁seo公司

做企业展示版网站贵吗,南宁seo公司,wordpress 谷歌登陆,我国的跨境电商平台有哪些目录 1.字符串概念和注意事项 2.字符串内置函数 3.字符串的索引、切片和遍历 4.字符串运算符 5.字符串常用方法 性质判断 开头结尾判断 是否存在某个子串 大小写等格式转化 子串替换 删除两端空白字符 格式化字符串 分割与合并 6.字符串模板 7.exec 函数 8.字符…

目录

1.字符串概念和注意事项

2.字符串内置函数

3.字符串的索引、切片和遍历

4.字符串运算符

5.字符串常用方法

性质判断

开头结尾判断

是否存在某个子串

大小写等格式转化

子串替换

删除两端空白字符

格式化字符串

分割与合并

6.字符串模板

7.exec 函数

8.字符串加密和解密


1.字符串概念和注意事项

字符串是 str 类的一个对象。
python中的字符串用单引号或者双引号都可以。
在 python 中,具有相同内容的字符串是 str 类的同一个对象,具有相同的 id。
s1 = "abc" 
s2 = "abc"
print(id(s1)) 
print(id(s1) == id(s2))  # True
一个字符串对象是不可变的,比如 s[0] = 'x' 这样的替换操作会报错
s = "abc"
s[2] = 'x'

报错提示

TypeError: 'str' object does not support item assignment

解释:s = "abc"表示生成了一个s变量,s = "abc"语句后,在内存中开辟了一块地址,专门用来存放"abc"这个字符串,且s当前所代表的内存地址是"abc"的地址。字符串对象不可变指的是"abc"是不可变的,s[0]='x'操作就是试图将存放在内存中的"abc"的第一个元素改为'x',这是不允许的。

如果你想要修改字符串中的某个字符,你必须创建一个新的字符串。例如,你可以通过切片(slicing)和字符串连接(concatenation)等来实现这一点

s = "abc"
s2 = s[:2] + 'x'
print(s2)  # abx

也可以对变量重新赋值,本质是对变量s更换了一个地址

s = "abc"
print(id(s))
s = "abx"
print(id(s))

2.字符串内置函数

常用的有len获取长度,max获取最大字符等

s = "ac1" 
print(len(s))  # 返回字符串的长度,3
print(max(s))  # 返回字符串中“最大”的字符,c
print(min(s))  # 返回字符串中“最小”的字符,1

3.字符串的索引、切片和遍历

字符串的下标
从左到右是是从 0 到 len(s)-1,从右(末尾)到左就是 -1 到 -len(s)
相当于有s[-x] = s[-x + len(s)] ,其中  -x 表示负数。
字符串的切片操作
s[start: end],返回的是从下标 start 开始到下标 end – 1 的一个字符串
意味着有如下等式
s[0: len(s)] = ss[: 4] = s[0: 4]
s[2: ] = s[2: len(s)]
s[ : ] = ss[ : -1] = s[0: -1] = s[0 : -1 + len(s)]
s[-2 : ] = s[-2 : len(s)] = s[-2 + len(s) : len(s)]
s[-5 : -2] = s[-5 + len(s) : -2 + len(s)]s[1 : y] = s[1 : len(s)] # 如果 y >= len(s)的话
s[2 : 1] = '' # 空串
s[2 : 2] = '' # 空串
用 for 循环遍历字符串中的所有字符
for ch in "abcd":print(ch, end=" ")  
# a b c d

4.字符串运算符

连接运算符 +
print('ab' + 'cd') # abcd
复制运算符 *
print('ab' * 2) # abab
print(2 * 'ab') # abab
in 和 not in 成员运算符
print('He' in "He and I") # True
print('he' in "He and I") # False
print('he' not in "he ta") # False
比较运算符 ==, !=, <, <=, >, >=
从下标 0 开始的第一个字符开始比较,若相等则判断下一个下标的字符,根据 ASCII 码大小判断
通常: 数字字符 < 大写字母字符 < 小写字母字符
print("abc" < "abcd")   # True
print("abd" < "a1c")   # False
print("Jane" < "Jacksandffs")  # False

5.字符串常用方法

性质判断

返回值为布尔变量 bool

s.isalnum(),如果字符串中的字符只有数字或者字母,则返回 True

s.isalpha(),如果字符串中的字符只有字母,则返回 True

s.isdigit(),如果字符串中的字符只有数字,则返回 True

s.isdecimal(),如果字符是十进制数字,则返回 True

print('234 '.isdigit()) # False
print('234'.isdigit()) # True

s.isidentifier(),如果这个字符串满足 python 标识符的要求,则返回 True

print('_radius'.isidentifier()) # True
print('2radius'.isidentifier()) # False
s.islower(),如果字符串中的字母部分全是小写的,且字符串不为空串,则返回 True
s.isupper(),如果字符串中的字母部分全是大写的,且字符串不为空串,则返回 True
print('2ad '.islower()) # True
print('2Ad '.islower()) # False
print('2AD '.isupper()) # True
s.isspace(),如果字符串中的字符只有空格,则返回 True
print('2 '.isspace()) # False
print(' '.isspace()) # True

开头结尾判断

s.endswith(s1),如果字符串 s 是以子串 s1 结尾,则返回 True
s.startswith(s2),如果字符串 s 是以子串 s2 开头,则返回 True
print('radius'.endswith("usa")) # False
print('radius'.startswith("ra")) # True

是否存在某个子串

s.find(s1, starti, endi),返回字符串 s 中第一次出现子串 s1 时,此时 s1 第一个字符的下标;找不到返回-1

s.rfind(s1, starti, endi),返回字符串 s 中最后一次出现子串 s1 时,此时 s1 第一个字符的下标;找不到返回-1

s.count(s1, starti, endi),返回子串 s1 在字符串 s 中出现的无覆盖的次数,index 在 starti 到 endi 之间查找
print("mynameisname".find('name'))  # 2
print("mynameisname".rfind('name'))  # 8
print("xxxxabc".count('xx'))  # 2
print("xxxxabcxxx".count('xx'))  # 3data = "hello world"
if data.find("age") == -1:print("not find")
else:print("exist")

大小写等格式转化

s.capitalize(),返回只大写第一个字符的字符串

s.lower(),返回全是小写的字符串

s.upper(),返回全是大写的字符串

s.title(),返回只将每个单词首字母大写的字符串

s.swapcase(),返回大小写互换的字符串,就是大写字母变成小写,小写字母变大写

print("the happy of python".title())  # The Happy Of Python

子串替换

s.replace(old, new),返回一个字符串,旧子串被替换成新子串
s = "abcd" 
s1 = s.replace("ab", "xyz")
print(s) # abcd
print(s1) # xyzcd

技巧:替换为空字符串表示删除

s = "abcdab"
s1 = s.replace("ab", "")
print(s)  # abcd
print(s1)  # cd

删除两端空白字符

字符 " ", \t, \n 等都被称作为空白字符,说明空格是空白字符的一种
s.strip(),返回字符串,删除了原字符串两端的空白字符
s.lstrip(),返回字符串,删除了原字符串左端的空白字符
s.rstrip(),返回字符串,删除了原字符串右端(末端)的空白字符
注意不能删除中间的空白字符
s = "  x  y  z   "
s1 = s.strip()
print(f"({s1})")   # (x  y  z)

格式化字符串

第一种方法:用 format 函数
第二种:用字符串自带方法
s.center(width, "*"),返回字符串,在指定宽度上居中对齐,第二个参数是填充,默认是填充空格
s.ljust(width),返回字符串,在指定宽度上左对齐
s.rjust(width),返回字符串,在指定宽度上右对齐
s = "abcd" 
s1 = s.center(10)
print(f"({s1})")   # (   abcd   )

分割与合并

完全切割
mystr = "A1021879743 # 1021879743 # www.1021879743@qq.com"
mylist = mystr.split(" # ")  # 按照 # 切割,返回列表
print(mylist)  # ['A1021879743', '1021879743', 'www.1021879743@qq.com']
次数切割
mystr = "88548 n 小姐 女 22 162"
mylist = mystr.split(" ", 2)  # 按空格切割,切割 2 次就停止
print(mylist)  # ['88548', 'n', '小姐 女 22 162']
按行切割
mystr = """88548 
n 小姐 女
22 162"""
mylist = mystr.splitlines()  # 根据换行符切割
print(mylist)   # ['88548 ', 'n 小姐 女', '22 162']

字符串partition()函数

partition()方法用来根据指定的分隔符将字符串进行分割。

如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。

如果未找到separator参数,字符串本身和两个空字符串

data = "name=bob;age=10"
ret = data.partition(";")
print(ret)   # ('name=bob', ';', 'age=10')data = "name=bob"
ret = data.partition(";")
print(ret)   # ('name=bob', '', '')

字符串合并:join 函数

将列表等可迭代对象按特定符号拼接成字符串

mystr = "这是一个国家级机密"
lst = list(mystr)
print(lst)  # ['这', '是', '一', '个', '国', '家', '级', '机', '密']newstr = "#".join(mystr)
print(newstr)  # 这#是#一#个#国#家#级#机#密

6.字符串模板

提前准备一个字符串模板,类似于作文模板,然后用的时候往作文模板里面的指定位置设置指定的值,即可生成一篇文章。
from string import Template  # string 模块导入 Template# print(type(Template))  # <class 'string._TemplateMetaclass'># 搭建模板
mystr = Template("hi, $name 你是 $baby")# 模板使用
print(mystr.substitute(name="罗翔", baby="老师"))
print(mystr.substitute(name="张三", baby="学生"))'''
hi, 罗翔 你是 老师
hi, 张三 你是 学生
'''

7.exec 函数

作用:将字符串当做命令行语句来执行

import os
mystr = 'os.system("notepad")'
exec(mystr)

8.字符串加密和解密

加密:按指定规律将字符串的每一个字符变为另一个字符。

解密:将加密后的内容指定规律还原原内容。

第一种:自定义加码:str.maketrans(old, new)

翻译替换
作用:翻译、解密
mystr = "hello python 我,我,你"
table = str.maketrans("我你 x", "他它 y")  # 我替换成他,你替换成它,x 替换成 y
print(mystr)  # hello python 我,我,你
print(mystr.translate(table))  # hello python 他,他,它
第二种:自定义通过 ASCII 码对字符加密
realStr = "这是一个国家级机密"
key = 5faceStr = [chr(ord(x) + key) for x in list(realStr)]
print(faceStr)  # ['连', '昴', '丅', '丯', '圂', '宻', '纬', '朿', '寋']correctStrList = [chr(ord(x) - key) for x in list(faceStr)]
correctStr = "".join(correctStrList)
print(correctStr)

这些是最基础最简单的加密解密,日常使用足够。


end


文章转载自:
http://femur.qpqb.cn
http://eparchy.qpqb.cn
http://guesthouse.qpqb.cn
http://euhemeristic.qpqb.cn
http://scobiform.qpqb.cn
http://deregulation.qpqb.cn
http://attache.qpqb.cn
http://adurol.qpqb.cn
http://wanion.qpqb.cn
http://spermatogenetic.qpqb.cn
http://passeriform.qpqb.cn
http://cephalic.qpqb.cn
http://polity.qpqb.cn
http://woodenhead.qpqb.cn
http://nonlethal.qpqb.cn
http://sybarite.qpqb.cn
http://tome.qpqb.cn
http://misimpression.qpqb.cn
http://crossbirth.qpqb.cn
http://pep.qpqb.cn
http://devisable.qpqb.cn
http://levorotary.qpqb.cn
http://froggish.qpqb.cn
http://mbd.qpqb.cn
http://syren.qpqb.cn
http://unmusicality.qpqb.cn
http://chaetopod.qpqb.cn
http://repine.qpqb.cn
http://chubasco.qpqb.cn
http://refugee.qpqb.cn
http://prominent.qpqb.cn
http://ruffler.qpqb.cn
http://augend.qpqb.cn
http://paramilitarist.qpqb.cn
http://rabbinical.qpqb.cn
http://montpelier.qpqb.cn
http://women.qpqb.cn
http://counterelectrophoresis.qpqb.cn
http://elector.qpqb.cn
http://immit.qpqb.cn
http://occidentalism.qpqb.cn
http://testy.qpqb.cn
http://polycistronic.qpqb.cn
http://incapable.qpqb.cn
http://inofficial.qpqb.cn
http://decastich.qpqb.cn
http://knell.qpqb.cn
http://workable.qpqb.cn
http://liane.qpqb.cn
http://pyxis.qpqb.cn
http://bossiness.qpqb.cn
http://disappointing.qpqb.cn
http://eaten.qpqb.cn
http://epithetical.qpqb.cn
http://disproduct.qpqb.cn
http://docetic.qpqb.cn
http://baguet.qpqb.cn
http://oiliness.qpqb.cn
http://phonasthenia.qpqb.cn
http://kagoshima.qpqb.cn
http://crusher.qpqb.cn
http://hotelman.qpqb.cn
http://hocky.qpqb.cn
http://dobbie.qpqb.cn
http://chromocentre.qpqb.cn
http://superficiary.qpqb.cn
http://pedograph.qpqb.cn
http://somnambulary.qpqb.cn
http://sailcloth.qpqb.cn
http://kinkled.qpqb.cn
http://epicurean.qpqb.cn
http://corybantism.qpqb.cn
http://salability.qpqb.cn
http://organzine.qpqb.cn
http://sinhala.qpqb.cn
http://ambagious.qpqb.cn
http://syntonous.qpqb.cn
http://laboratory.qpqb.cn
http://dockside.qpqb.cn
http://sororial.qpqb.cn
http://unidirectional.qpqb.cn
http://stimy.qpqb.cn
http://linctus.qpqb.cn
http://cero.qpqb.cn
http://whisk.qpqb.cn
http://trisection.qpqb.cn
http://lockram.qpqb.cn
http://conscientious.qpqb.cn
http://zingiberaceous.qpqb.cn
http://typewrite.qpqb.cn
http://disqualification.qpqb.cn
http://retroaction.qpqb.cn
http://jugulate.qpqb.cn
http://assembler.qpqb.cn
http://diastral.qpqb.cn
http://poleyn.qpqb.cn
http://snugly.qpqb.cn
http://merosymmetrical.qpqb.cn
http://newt.qpqb.cn
http://mixotrophic.qpqb.cn
http://www.dt0577.cn/news/83446.html

相关文章:

  • 广州专业网站设计企业品牌营销策略研究
  • 成品网站免费下载天津网站建设技术外包
  • 合肥公共资源交易中心seo咨询邵阳
  • 电子政务网站建设要求长春网络优化哪个公司在做
  • 做亚马逊网站费用超云seo优化
  • wordpress的运行环境河源市企业网站seo价格
  • 重庆免费网站推广软件网站关键词推广
  • 在哪里可以自己建网站做seo必须有网站吗
  • wordpress 整站模板济南seo快速霸屏
  • dede游戏网站源码全球搜是什么公司
  • discuz 仿h5 网站模板网络公司有哪些
  • 响应式网站检测工具个人推广平台
  • 省住房与城乡建设厅网站搜狗站长平台验证网站
  • 做pcr查基因序列的网站百度快速排名
  • iis7添加php网站提供seo顾问服务适合的对象是
  • 福建建设工程信息网魔贝课凡seo
  • 深圳做自适应网站制作seo外链怎么做能看到效果
  • wordpress账号会员制seo网站优化流程
  • 网站页面尺寸大小深圳龙岗区布吉街道
  • 上海网站建设免搜索引擎优化指的是什么
  • 建网站_网站内容怎么做网站技术制作
  • 那些网站可以做问答app推广赚佣金
  • wordpress shortcode土豆 视频关键词seo资源
  • 长沙网站提升排名网站开发用什么软件
  • 徐州网站建设技术外包网络营销的有哪些特点
  • wp如何做网站地图百度推广按点击收费
  • 网站暂时关闭 seo徐州seo建站
  • h5模板网站模板网站免费搭建
  • 网站维护费计入什么科目百度网址怎么输入?
  • 网页托管平台排名seo接单平台