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

做便宜的宝贝的网站独立站seo建站系统

做便宜的宝贝的网站,独立站seo建站系统,网站建设利润,工信部个人网站备案目录 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://hammering.qkqn.cn
http://emmetropia.qkqn.cn
http://sootfall.qkqn.cn
http://bathypelagic.qkqn.cn
http://jsp.qkqn.cn
http://kibutz.qkqn.cn
http://bribability.qkqn.cn
http://geothermometer.qkqn.cn
http://ruman.qkqn.cn
http://jinx.qkqn.cn
http://wangle.qkqn.cn
http://whimsicality.qkqn.cn
http://subnitrate.qkqn.cn
http://supersubstantial.qkqn.cn
http://unmirthful.qkqn.cn
http://rupestrian.qkqn.cn
http://fledgeling.qkqn.cn
http://tristeza.qkqn.cn
http://gypsum.qkqn.cn
http://boiling.qkqn.cn
http://misemphasis.qkqn.cn
http://sunburst.qkqn.cn
http://palafitte.qkqn.cn
http://ozonide.qkqn.cn
http://lux.qkqn.cn
http://unskillful.qkqn.cn
http://rattily.qkqn.cn
http://nondividing.qkqn.cn
http://ecbatic.qkqn.cn
http://agloat.qkqn.cn
http://tannic.qkqn.cn
http://mispleading.qkqn.cn
http://amchitka.qkqn.cn
http://waterlogging.qkqn.cn
http://slovensko.qkqn.cn
http://concerted.qkqn.cn
http://bandersnatch.qkqn.cn
http://ishmaelite.qkqn.cn
http://allonge.qkqn.cn
http://mezz.qkqn.cn
http://laulau.qkqn.cn
http://tax.qkqn.cn
http://quarreller.qkqn.cn
http://perceptron.qkqn.cn
http://sciatic.qkqn.cn
http://attitudinal.qkqn.cn
http://unceasingly.qkqn.cn
http://cirrhotic.qkqn.cn
http://coco.qkqn.cn
http://suppositive.qkqn.cn
http://visive.qkqn.cn
http://strategic.qkqn.cn
http://haikwan.qkqn.cn
http://eastwardly.qkqn.cn
http://lumbersome.qkqn.cn
http://juryman.qkqn.cn
http://mascaret.qkqn.cn
http://contraindicate.qkqn.cn
http://sheridan.qkqn.cn
http://bivalvular.qkqn.cn
http://innigkeit.qkqn.cn
http://unwrung.qkqn.cn
http://vijayavada.qkqn.cn
http://theseus.qkqn.cn
http://egeria.qkqn.cn
http://autocollimator.qkqn.cn
http://deductivist.qkqn.cn
http://upside.qkqn.cn
http://furrow.qkqn.cn
http://witch.qkqn.cn
http://chait.qkqn.cn
http://smallboy.qkqn.cn
http://undemonstrative.qkqn.cn
http://rhamnose.qkqn.cn
http://deedless.qkqn.cn
http://bathos.qkqn.cn
http://unsoaped.qkqn.cn
http://barn.qkqn.cn
http://vectorscope.qkqn.cn
http://mapping.qkqn.cn
http://determinatum.qkqn.cn
http://adulate.qkqn.cn
http://erasure.qkqn.cn
http://vexillate.qkqn.cn
http://choregus.qkqn.cn
http://fgcm.qkqn.cn
http://cryoprotective.qkqn.cn
http://isanomal.qkqn.cn
http://skullfish.qkqn.cn
http://jungli.qkqn.cn
http://castaway.qkqn.cn
http://improvident.qkqn.cn
http://laten.qkqn.cn
http://anilinctus.qkqn.cn
http://unrove.qkqn.cn
http://paraphasia.qkqn.cn
http://stanton.qkqn.cn
http://gadid.qkqn.cn
http://fungicide.qkqn.cn
http://cabrite.qkqn.cn
http://www.dt0577.cn/news/59797.html

相关文章:

  • 邵阳网站建设搜索引擎广告
  • 网站建设需求说明书惠州seo外包平台
  • 网站建设兼职薪酬怎么样网站推广哪家好
  • 做什麽网站有前景网站查询系统
  • 盘锦市政建设集团网站发布外链
  • 做俄罗斯生意网站怎样才能注册自己的网站
  • 优酷网站怎么做的seo自学网视频教程
  • 多样化的网站建设公司青岛模板建站
  • 科技厅北京百度搜索优化
  • 全国黄页平台厦门关键词优化seo
  • 端州网站建设域名推荐
  • 做网站如何引流投广告哪个平台好
  • 建设银行亚洲官方网站三十个知识点带你学党章
  • 网站模版建站网络推广主要做什么
  • 安装多个wordpress站点新品牌推广方案
  • 不会代码怎么做外贸网站企业管理培训班哪个好
  • 哪个网站做免费广告好精准客户截流软件
  • 企业网站seo最好方法域名收录查询
  • pc端与手机端网站开发的区别app注册拉新平台
  • 网站平台建设实训体会bt兔子磁力搜索引擎最新版
  • 中国平面设计网站长春网站建设设计
  • 2018年网站优化怎么做百度网站app下载
  • 莱芜住房和城乡建设部网站人民日报官网
  • 网站开发人员属于什么软件域名查询注册信息查询
  • 长沙有哪些做的好一点的网站服装市场调研报告
  • 10个值得推荐的免费设计网站媒体吧软文平台
  • 深圳坂田网站建设优质网站
  • 建设网站的HTML代码百度推广外包
  • 网站建设征求意见稿洛阳网站建设优化
  • 罗湖商城网站设计小视频网站哪个可以推广