一次替换字符串中的多个字符

时间:2017-10-08 09:23:48

标签: python

请告知以下代码是否有效。我似乎根本不工作

string = str(input('Enter something to change'))
replacing_words = 'aeiou'

for i in replacing_words:
    s = string.replace('replacing_words', ' ')

print(s)

我的意思是用空格替换字符串中的所有元音。 如果这是一个错误的代码,有人可以协助正确的代码和解释,为什么它不起作用?

谢谢

6 个答案:

答案 0 :(得分:5)

您可以定义translation table。这是一个Python2代码:

>>> import string
>>> vowels = 'aeiou'
>>> remove_vowels = string.maketrans(vowels, ' ' * len(vowels))
>>> 'test translation'.translate(remove_vowels)
't st tr nsl t  n'

它快速,简洁,不需要任何循环。

对于Python3,你会写:

'test translation'.translate({ord(ch):' ' for ch in 'aeiou'}) # Thanks @JonClements.

答案 1 :(得分:2)

  • 您正在使用文字'replacement_words'而不是for-loop中的变量i。
  • 您不会替换原始字符串以再次修改它,而是创建一个新字符串,导致仅显示最后一个替换

这是正确的代码。

string = input('Enter something to change')
vowels = 'aeiouy'

for i in vowels:
    string = string.replace(i, ' ')

print(string)

另外,我认为输入返回一个字符串'type'。所以调用str将没有任何效果。不确定。另外#2:y也是一个元音(如果你想彻底的话,åäö以及其他变音符号和奇怪的字符也是如此)。

答案 2 :(得分:1)

您错误地使用了btnSubmit.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { try { int count = table.getRowCount(); String driver = "oracle.jdbc.driver.OracleDriver"; Class.forName(driver); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott", "tiger"); conn.setAutoCommit(false); PreparedStatement pst = conn.prepareStatement("Insert into students(ROLL_NO,NAME,MID1,MID2,Marksinwords) values (?,?,?,?,?)"); for(int row = 0; row<count; row++) { String roll = (String)table.getValueAt(row, 0); String name = (String)table.getValueAt(row, 1); String MARKS1 = (String)tableModel.getValueAt(row, 2); String MARKS2 = (String)table.getValueAt(row, 3); String marksinwords = (String)table.getValueAt(row, 4); pst.setString(1, roll); pst.executeUpdate(roll); pst.setString(2, name); pst.executeUpdate(name); pst.setString(3,MARKS1 ); pst.executeUpdate(MARKS1); pst.setString(4,MARKS2 ); pst.executeUpdate(MARKS2); pst.setString(5, marksinwords); pst.executeUpdate(marksinwords); pst.addBatch(); } pst.executeBatch(); conn.commit(); } catch (Exception ex) { Logger.getLogger(FacultyTableSubmit.class.getName()).log(Level.SEVERE, null, ex); } } }); 方法。由于您希望分别替换每个字符,因此每次都应传递一个字符。

这是一个可以解决问题的单行:

replace

答案 3 :(得分:1)

试试这段代码:

string=raw_input("Enter your something to change")
replacing_words = 'aeiou'
for m in replacing_words:
    string=string.replace(m, ' ')
print string

答案 4 :(得分:0)

在Python中,字符串是不可变的。

# Python 3.6.1

""" Replace vowels in a string with a space """

txt = input('Enter something to change: ')
vowels = 'aeiou'
result = ''

for ch in txt:
    if ch.lower() in vowels:
        result += ' '
    else:
        result += ch

print(result)

测试它:

Enter something to change: English language
 ngl sh l ng  g

在Python 3.x中,您也可以编写(无需导入):

vowels = 'aeiouAEIOU'
space_for_vowel = str.maketrans(vowels, ' ' * len(vowels))
print('hello wOrld'.lower().translate(space_for_vowel))

h ll  w rld

答案 5 :(得分:0)

如果单词是元音字符串,则可以先检查字符串然后替换:

string = str(input('Enter something to change'))

replacing_words = 'aeiou'

for i in string:
    if i in replacing_words:
        string=string.replace(i," ")

print(string)

如果你想保留原始副本并想要更改字符串,那么:

string = str(input('Enter something to change'))
string1=string[:]
replacing_words = 'aeiou'

for i in string1:
    if i in replacing_words:
        string1=string1.replace(i," ")

print("{} is without vowels : {} ".format(string,string1))

输出:

Enter something to change Batman
Batman is without vowels : B tm n