python字符串操作函数,split,join,replace

a=[1,2,3.4,5]
print(a)
[ 1 2 3 4 5 ]

print(a[-1]) ###取最后一个元素
[5]

print(a[:-1]) ### 除了最后一个取全部
[ 1 2 3 4 ]

print(a[::-1]) ### 取从后向前(相反)的元素
[ 5 4 3 2 1 ]

print(a[2::-1]) ### 取从下标为2的元素翻转读取
[ 3 2 1 ]
 

1.按某一个字符分割,如‘.’

>>> s = ('www.google.com')
>>> print(s)
www.google.com

>>> s.split('.')
['www', 'google', 'com']

2.按某一个字符分割,且分割n次。如按‘.'分割1次;参数maxsplit位切割的次数

>>> s = 'www.google.com'
>>> s
'www.google.com'

>>> s.split('.', maxsplit=1)
['www', 'google.com']

3.按某一字符串分割。如:‘||’

>>> s = 'WinXP||Win7||Win8||Win8.1'

>>> s

'WinXP||Win7||Win8||Win8.1'

>>> s.split('||')

['WinXP', 'Win7', 'Win8', 'Win8.1']

’ '.join(str)

Python中有join()和os.path.join()两个函数,具体作用如下:

join(): 连接字符串数组。将字符串、元组、列表中的元素以指定的字符(分隔符)连接生成一个新的字符串

os.path.join(): 将多个路径组合后返回

对序列进行操作(分别使用’ ‘与’:'作为分隔符)

>>> seq1 = ['hello','good','boy','doiido']

>>> print ' '.join(seq1)

hello good boy doiido

>>> print ':'.join(seq1)

hello:good:boy:doiido

对字符串进行操作

>>> seq2 = "hello good boy doiido"

>>> print ':'.join(seq2)

h:e:l:l:o: :g:o:o:d: :b:o:y: :d:o:i:i:d:o

    1
    2
    3
    4
    5

对元组进行操作

>>> seq3 = ('hello','good','boy','doiido')

>>> print ':'.join(seq3)

hello:good:boy:doiido

对字典进行操作

>>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}

>>> print ':'.join(seq4)

boy:good:doiido:hello

合并目录

>>> import os

>>> os.path.join('/hello/','good/boy/','doiido')

'/hello/good/boy/doiido'

str.strip()

声明:s为字符串,rm为要删除的字符序列

s.strip(rm) 删除s字符串中开头、结尾处,位于 rm删除序列的字符;

s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的字符;

s.rstrip(rm) 删除s字符串中结尾处,位于 rm删除序列的字符;

    当rm为空时,默认删除空白符(包括’\n’, ‘\r’, ‘\t’, ’ ')

例如:

>>> a = '123abc'

>>> a.strip('21')

‘3abc’ 结果是一样的

>>> a.strip('12')

'3abc'

2.这里的rm删除序列是只要边(开头或结尾)上的字符在删除序列内,就删除掉。

例如 :

>>> a = '123abc'

>>> a.strip('21')

‘3abc’ 结果是一样的

>>> a.strip('12')

'3abc'
 

请将 a 字符串的数字取出,并输出成一个新的字符串。

a= "As3eAF"
c=''.join([s for s in a if s.isdigit()])
print(c)

    请统计 a 字符串中每个字母的出现次数(忽略大小写,a 与 A 是同一个字母),并输出成一个字典。 例{‘a’: 3, ‘s’: 1,
    ‘3’: 1, ‘e’: 1, ‘f’: 1}

a= "As3eAF"
a= a.lower()
c=dict([(x,a.count(x))for x in set(a)])
print(c)

    请去除 a 字符串多次出现的字母,仅留最先出现的一个,大小写不敏感。例’ aAs3eAF’,经过去除后,输出 ’ aAs3eF ’

a = "aAs3eAF"

string2 = ''.join(list(set(a.lower())))

print(string2)

    按 a 字符串中字符出现频率从高到低输出到列表,如果次数相同则按字母顺序排列。

class charAndCount:
    def __init__(self,ch=None,count=None):
        self.ch=ch
        self.count=count
s= "aAs3eAF"
l=[]
for c in s :
    cc = [m for m in l if m.ch==c]#在l中需找符合条件 m.ch=c的字符串
    if len(cc)==0:
        l.append(charAndCount(ch=c,count=1))
    else:
        cc.__getitem__(0).count +=1
print("按输入顺序")
for n in l:
    print(n.ch,"=",n.count)
print("按字符出现次数")
for n in sorted(l,key=lambda x : -x.count):
    print(n.ch, "=", n.count)
print("按字符顺序(a.b.c..的顺序)排版")
for n in sorted(l,key=lambda x :(-x.count,x.ch)):
print(n.ch, "=", n.count)

    已知 a = [1,2,3,6,8,9,10,14,17],请将该list转换为字符串,例如 ‘123689101417’

a = [1,2,3,6,8,9,10,14,17]
list1=map(str,a)
print("".join(list1))

    编写函数,接收一句英文,把其中的单词倒置,标点符号不倒置,例如:I like Beijing.经过函数后变为:beijing. like I


def rev(s):
    return ' '.join(reversed(s.split())) x=rev('I like beijing.') print(x)

    编写程序,要求输入一个字符串,然后输入一个整数作为凯撒加密算法的密钥,然后输出该字符串加密后的结果

key = input("请输入加密密钥: ")
enc = input("请输入要加密的字符: ")
dec = ord(key) ^ ord(enc)      
print("加密结果:",chr(dec))

s为字符串
s.isalnum() 所有字符都是数字或者字母
s.isalpha() 所有字符都是字母
s.isdigit() 所有字符都是数字
s.islower() 所有字符都是小写
s.isupper() 所有字符都是大写
s.istitle() 所有单词都是首字母大写,像标题
s.isspace() 所有字符都是空白字符、\t、\n、\r

判断是整数还是浮点数
a=123
b=123.456

isinstance(a,int)
True
isinstance(b,float)
True
isinstance(b,int)
False
 

Python中提供的 replace() 函数的作用是用一个新字符或字符串替换字符串中某个字符串中的原有的字符或子串。Python中 replace() 函数有两种使用形式,一种是简单替换,即使用新字符串替换原字符串中全部与之匹配的子串;另外一种是在替换中指定替换的次数。

一、Python中replace()函数的语法格式

str.replace(old, new [, count])

str 是要执行替换的字符串或字符串变量,各参数的含义如下:

old : str 中要被替换的旧字符串;

new : 用于替换 str 中的新字符串;

count : 可选参数,指定 count 后,只有 str 中前 count 个旧字符串old被替换。

该函数执行完毕后,将生成替换后的字符串。

二、replace() 函数使用示例

1、简单使用

str1 = "小华喜欢小刚,小刚喜欢小花,小花喜欢小华"

old_str = "喜欢"

new_str = "打了"

res = str1.replace(old_str, new_str)

print(str1)

print(res)

输出结果:

小华喜欢小刚,小刚喜欢小花,小花喜欢小华

小华打了小刚,小刚打了小花,小花打了小华

上面这个例子就是把 str1 中所有的“喜欢”这个字符串被替换成了“打了”。同时,也应注意到,replace() 函数执行完后是生成一个字符串的副本,并没有影响原字符串的内容。

2、指定 count 参数

str1 = "Python is simple,Python is easy to learn,Python means everything"

res = str1.replace("Python", "Java", 2)

print(res)

输出结果:

Java is simple, Java is easy to learn, Python means everything

从结果可以看出,因为指定了 count 参数,这里只对str1中前 2 个找到的字符串"Python"进行了替换,而第 3 个没有被替换。

3、大小写敏感

在Python中,基本所有函数对字符串的处理都是大小写敏感的,replace() 函数也不例外。看下面的例子:

str1 = "Python is simple, python is complex, Python is open."

res = str1.replace( "python", "Java")

print(res)

输出结果:

Python is simple, Java is complex, Python is open.

从这里可以看出,该函数执行后,原来的"python is complex" 变成了 "Java is complex",而处于其他两个位置的"Python"没有被替换,这是因为只有第2个位置的 "python" 在大小形式上完全相同。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Marioo_JJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值