使用 string.replace(x, y) 替换所有
Using string.replace(x, y) to replace all
我刚刚开始学习 python 并希望在使用 string.replace(x,y) 时得到帮助。
具体来说,根据字母最初是否大写,将所有替换为 X's 和 x's。
例如
约翰·史密斯 -> Xxxx X. Xxxxx
下面是我创建的内容
print("Enter text to translate:", end ="")
sentence = input ()
replaced = sentence.replace("", "x")
print(replaced)
然而,当我输入像 "John Smith" 这样的文本时。我返回 "xJxoxhxnx xSx.x xSxmxixtxhx"。
提前致谢!
编辑:虽然 string.replace(x,y) 的执行时间可能更长,但我想在找到更快更短的方法来执行相同操作之前慢慢积累我的知识。如果用 string.replace(x, y) 而不是 re.sub
来解释,我将不胜感激
Edit2:我被告知 string.replace 是错误的工具。谢谢您的帮助!我将改为阅读 re.sub。
不是字符串替换的正确用例。
您可以做两件事:
- 遍历字符串并执行操作
- 使用 re.sub 替换使用正则表达式。 (How to input a regex in string.replace?)
** 编辑固定代码 **
试试这个,这是一个有点简单且容易破解的例子,但这是你需要实现你想要的:
s = "Testing This"
r = ""
for c in s:
if c.isalpha():
if c.islower():
r += "x"
else:
r += "X"
else:
r += c
print(r)
import re
print("Enter text to translate:", end ="")
sentence = input()
replaced = re.sub("[A-Z]", 'X', re.sub("[a-z]", 'x', sentence))
print replaced
使用re.sub替换字符串中的单个字符或遍历字符串。
简单的方法:
>>> my_string = "John S. Smith"
>>> replaced = ''
>>> for character in my_string:
if character.isupper():
replaced += 'X'
elif character.islower():
replaced += 'x'
else:
replaced += character
>>> replaced
'Xxxx X. Xxxxx'
一个班轮:
>>> ''.join('x' if c.islower() else 'X' if c.isupper() else c for c in my_string)
'Xxxx X. Xxxxx'
您可以使用 re
模块,并提供 re.sub
回调函数来实现此目的。
import re
def get_capital(ch):
if ch.isupper():
return "X"
return "x"
def get_capitals(s):
return re.sub("[a-zA-Z]", lambda ch: get_capital(ch.group(0)), s)
print(get_capitals("John S. Smith"))
有输出:
Xxxx X. Xxxxx
作为一种风格问题,我没有将其中的任何内容压缩成 lambda 表达式或单行代码,尽管您可能可以。
这比仅仅重复串联要快得多。
如果您坚持使用 replace
,即使它是错误的工具(因为它一次只能替换一个字母并且每次都必须遍历整个字符串),这里有一个方法:
>>> s = 'John S. Smith'
>>> for c in s:
if c.islower():
s = s.replace(c, 'x')
if c.isupper():
s = s.replace(c, 'X')
>>> s
'Xxxx X. Xxxxx'
还有一种更简洁更有效的方法:
>>> ''.join('x' * c.islower() or 'X' * c.isupper() or c for c in s)
'Xxxx X. Xxxxx'
和正则表达式方式:
>>> re.sub('[A-Z]', 'X', re.sub('[a-z]', 'x', s))
'Xxxx X. Xxxxx'
print("Enter text to translate:", end ="")
sentence = input ()
replaced = ''.join(['x' if (i>='a' and i<='z') else 'X' if (i>='A' and i<='Z') else i for i in sentence])
print(replaced)
我刚刚开始学习 python 并希望在使用 string.replace(x,y) 时得到帮助。
具体来说,根据字母最初是否大写,将所有替换为 X's 和 x's。
例如 约翰·史密斯 -> Xxxx X. Xxxxx
下面是我创建的内容
print("Enter text to translate:", end ="")
sentence = input ()
replaced = sentence.replace("", "x")
print(replaced)
然而,当我输入像 "John Smith" 这样的文本时。我返回 "xJxoxhxnx xSx.x xSxmxixtxhx"。
提前致谢!
编辑:虽然 string.replace(x,y) 的执行时间可能更长,但我想在找到更快更短的方法来执行相同操作之前慢慢积累我的知识。如果用 string.replace(x, y) 而不是 re.sub
来解释,我将不胜感激Edit2:我被告知 string.replace 是错误的工具。谢谢您的帮助!我将改为阅读 re.sub。
不是字符串替换的正确用例。 您可以做两件事:
- 遍历字符串并执行操作
- 使用 re.sub 替换使用正则表达式。 (How to input a regex in string.replace?)
** 编辑固定代码 **
试试这个,这是一个有点简单且容易破解的例子,但这是你需要实现你想要的:
s = "Testing This"
r = ""
for c in s:
if c.isalpha():
if c.islower():
r += "x"
else:
r += "X"
else:
r += c
print(r)
import re
print("Enter text to translate:", end ="")
sentence = input()
replaced = re.sub("[A-Z]", 'X', re.sub("[a-z]", 'x', sentence))
print replaced
使用re.sub替换字符串中的单个字符或遍历字符串。
简单的方法:
>>> my_string = "John S. Smith"
>>> replaced = ''
>>> for character in my_string:
if character.isupper():
replaced += 'X'
elif character.islower():
replaced += 'x'
else:
replaced += character
>>> replaced
'Xxxx X. Xxxxx'
一个班轮:
>>> ''.join('x' if c.islower() else 'X' if c.isupper() else c for c in my_string)
'Xxxx X. Xxxxx'
您可以使用 re
模块,并提供 re.sub
回调函数来实现此目的。
import re
def get_capital(ch):
if ch.isupper():
return "X"
return "x"
def get_capitals(s):
return re.sub("[a-zA-Z]", lambda ch: get_capital(ch.group(0)), s)
print(get_capitals("John S. Smith"))
有输出:
Xxxx X. Xxxxx
作为一种风格问题,我没有将其中的任何内容压缩成 lambda 表达式或单行代码,尽管您可能可以。
这比仅仅重复串联要快得多。
如果您坚持使用 replace
,即使它是错误的工具(因为它一次只能替换一个字母并且每次都必须遍历整个字符串),这里有一个方法:
>>> s = 'John S. Smith'
>>> for c in s:
if c.islower():
s = s.replace(c, 'x')
if c.isupper():
s = s.replace(c, 'X')
>>> s
'Xxxx X. Xxxxx'
还有一种更简洁更有效的方法:
>>> ''.join('x' * c.islower() or 'X' * c.isupper() or c for c in s)
'Xxxx X. Xxxxx'
和正则表达式方式:
>>> re.sub('[A-Z]', 'X', re.sub('[a-z]', 'x', s))
'Xxxx X. Xxxxx'
print("Enter text to translate:", end ="")
sentence = input ()
replaced = ''.join(['x' if (i>='a' and i<='z') else 'X' if (i>='A' and i<='Z') else i for i in sentence])
print(replaced)