-3

How do you ignore a single character or symbol in a string

I want to ignore the ! and / in text or actually just the first character no matter what it was.

For example, something like this:

text = ['!kick', '/ban']

the output should look like this:

>> kick
>> ban

instead of:

>> !kick
>> /ban
kaya3
  • 41,043
  • 4
  • 50
  • 79
Frederik
  • 389
  • 1
  • 3
  • 19
  • 1
    Have you tried any solutions? Also, are you writing an IRC bot? If so, please state in the question. Finally, elaborate more on your scenario. – wei2912 Sep 12 '13 at 16:01

4 Answers4

2
text = ['!kick', '/ban', '!k!ck']
for s in text:
    print s[0].translate(None, '!/') + s[1:]

output:
kick
ban
k!ck

In the second parameter of translate() put all of the characters you want to get rid of.

Read more about translate()

Tyler
  • 16,759
  • 10
  • 49
  • 87
  • For future visitors: translate() works way differently in Python3. And Python2 is no longer supported. New link: https://docs.python.org/3/library/stdtypes.html#str.translate – bobsbeenjamin Dec 10 '20 at 02:02
1

To remove a specific char:

s=s.replace("!","") #!4g!hk becomes 4ghk

To remove 1st char:

s=s[1:]
Nacib Neme
  • 839
  • 1
  • 18
  • 28
1

Since you want to remove certain characters in the first position of the string, I'd suggest using str.lstrip().

for cmd in ['!kick', '/ban']:
    print cmd.lstrip('!/')
kindall
  • 168,929
  • 32
  • 262
  • 294
-2

Just use python's replace function:

for elem in ['!kick', '/ban']:
  print elem.replace('!','').replace('/','')

Output should look like this:

>> kick
>> ban
TabeaKischka
  • 743
  • 6
  • 14
  • Sorry, I forgot the '' in the second replace command. However, Python's Error message "TypeError: replace() takes at least 2 arguments (1 given)" is quite easy to understand, so you could have guessed that yourself – TabeaKischka Sep 13 '13 at 11:44