qhnw.net
当前位置:首页 >> python字符串组合 >>

python字符串组合

python替换某个文本中的字符串,然后生成新的文本文档,代码如下:import osos.chdir('d:\\') # 跳到d盘if not os.path.exists('test1.txt'): # 看一下这个文件是否存在exit(-1) #不存在就退出lines = open('test1.txt').readlines() #打开文件,读入每

import res = 'abcdefghijkl42819iwlh'm = re.findall(r'[a-eg-za-eg-z]+', s)print(m)运行结果是['abcde', 'ghijkl', 'iwlh']除了f以外的所有字母都匹配到了

>>> import itertools >>> srcstr = 'abcd' >>> [''.join(x) for x in itertools.permutations(src, 2)] ['ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc'] >>> [''.join(x) for x in itertools.permutations(src, 3)] ['abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca',

如果是字符串直接+就是了.a="a"b="b"c=a+bprint c 如果解决了您的问题请采纳!如果未解决请继续追问

a = "12345678"b = "abcdefg"#生成所2113有组5261合41021653result = [(color, size) for color in a for size in b]import random#随机选取版一个权组合print(random.choice(a),random.choice(b))

这是python中的字符串的功能.其实当使用加号运算符的时候会调用这个类的__add__()函数,这个函数是每个类都有的,对于自定义的类,不重写这个方法,+这个运算符就没作用.你也可以重写这个运算符来得到不同的功能.以下是示例

去掉##就可以了试一试看行不行##def removeDup(s):## word=''## words = s.split(' ')## i = 0## while i<len(words):## if (word != words[i]):## word = words[i]## i=i+1## else:## word = words[i]## del(words[i])## ## s=' '.join(words)## return s

#encoding: utf-8str = ["a","b","c","d","e"]result = []for x in ["a","b","c","d","e"]:for y in ["a","b","c","d","e"]:for z in ["a","b","c","d","e"]:for o in ["a", "b", "c", "d", "e"]:if (x != y ) and (x !

使用+ 就行print str1+str2

可以使用字符串格式化的函数format:name = "test"path=r'z:\auto\{0}\new.sh'.format(name)print(path)

网站首页 | 网站地图
All rights reserved Powered by www.qhnw.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com