例如,有一个字符串。的例子。
我怎样才能去掉中间的字符,即M ?我不需要密码。我想知道:
Python中的字符串是否以特殊字符结尾? 哪个是更好的方法-从中间字符开始将所有内容从右向左移动或创建一个新字符串而不复制中间字符?
例如,有一个字符串。的例子。
我怎样才能去掉中间的字符,即M ?我不需要密码。我想知道:
Python中的字符串是否以特殊字符结尾? 哪个是更好的方法-从中间字符开始将所有内容从右向左移动或创建一个新字符串而不复制中间字符?
这可能是最好的方法:
original = "EXAMPLE"
removed = original.replace("M", "")
不要担心转换字符之类的问题。大多数Python代码发生在更高的抽象级别上。
在Python中,字符串是不可变的,所以你必须创建一个新的字符串。对于如何创建新字符串,您有几个选项。如果你想移除“M”出现的地方:
newstr = oldstr.replace("M", "")
如果你想删除中心字符:
midlen = len(oldstr) // 2
newstr = oldstr[:midlen] + oldstr[midlen+1:]
你问字符串是否以特殊字符结尾。不,你像一个C程序员一样思考。在Python中,字符串以其长度存储,因此任何字节值,包括\0,都可以出现在字符串中。
我怎样才能去掉中间的字符,即M ?
你不能,因为Python中的字符串是不可变的。
Python中的字符串是否以特殊字符结尾?
不。它们类似于字符列表;列表的长度定义了字符串的长度,没有字符作为结束符。
哪个是更好的方法-从中间字符开始将所有内容从右向左移动或创建一个新字符串而不复制中间字符?
您不能修改现有的字符串,因此必须创建一个包含除中间字符以外的所有内容的新字符串。
在Python 2中,可以使用UserString。MutableString以可变的方式来做:
>>> import UserString
>>> s = UserString.MutableString("EXAMPLE")
>>> type(s)
<class 'UserString.MutableString'>
>>> del s[3] # Delete 'M'
>>> s = str(s) # Turn it into an immutable value
>>> s
'EXAPLE'
MutableString在Python 3中被移除。
字符串是不可变的。但是你可以把它们转换成一个可变的列表,然后在你改变它之后再把它转换回字符串。
s = "this is a string"
l = list(s) # convert to list
l[1] = "" # "delete" letter h (the item actually still exists but is empty)
l[1:2] = [] # really delete letter h (the item is actually removed from the list)
del(l[1]) # another way to delete it
p = l.index("a") # find position of the letter "a"
del(l[p]) # delete it
s = "".join(l) # convert back to string
您还可以创建一个新字符串,就像其他人演示的那样,从现有字符串中取出所需字符以外的所有内容。
def kill_char(string, n): # n = position of which character you want to remove
begin = string[:n] # from beginning to n (n not included)
end = string[n+1:] # n+1 through end of string
return begin + end
print kill_char("EXAMPLE", 3) # "M" removed
我在这里某处见过。
card = random.choice(cards)
cardsLeft = cards.replace(card, '', 1)
如何从字符串中删除一个字符: 下面是一个例子,其中有一堆卡片表示为字符串中的字符。 其中一个被绘制(为random.choice()函数导入random模块,它在字符串中选择一个随机字符)。 创建一个新的字符串cardsLeft来保存字符串函数replace()给出的剩余卡片,其中最后一个参数表示只有一个“card”将被空字符串替换…
如果你想删除/忽略字符串中的字符,例如,你有这个字符串,
“[11:L: 0]”
来自web API响应或类似的东西,比如CSV文件,假设你在使用请求
import requests
udid = 123456
url = 'http://webservices.yourserver.com/action/id-' + udid
s = requests.Session()
s.verify = False
resp = s.get(url, stream=True)
content = resp.content
循环并去除不需要的字符:
for line in resp.iter_lines():
line = line.replace("[", "")
line = line.replace("]", "")
line = line.replace('"', "")
可选的分割,你将能够单独读取值:
listofvalues = line.split(':')
现在访问每个值更容易了:
print listofvalues[0]
print listofvalues[1]
print listofvalues[2]
这将打印
11 l 0
from random import randint
def shuffle_word(word):
newWord=""
for i in range(0,len(word)):
pos=randint(0,len(word)-1)
newWord += word[pos]
word = word[:pos]+word[pos+1:]
return newWord
word = "Sarajevo"
print(shuffle_word(word))
删除一个字符或子字符串一次(只删除第一次):
main_string = main_string.replace(sub_str, replace_with, 1)
注意:这里的1可以用任何int替换,表示要替换的出现次数。
您可以简单地使用列表推导式。
假设你有字符串:my name is,你想删除字符m.使用以下代码:
"".join([x for x in "my name is" if x is not 'm'])
另一种方法是用一个函数,
下面是通过调用函数从字符串中删除所有元音的方法
def disemvowel(s):
return s.translate(None, "aeiouAEIOU")
Python 3.9+中引入了两个新的字符串删除方法
#str.removeprefix("prefix_to_be_removed")
#str.removesuffix("suffix_to_be_removed")
s='EXAMPLE'
在这个例子中,M的位置是3
s = s[:3] + s[3:].removeprefix('M')
OR
s = s[:4].removesuffix('M') + s[4:]
#output'EXAPLE'