-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
  • 47,440
  • 4
  • 68
  • 97
Frederik
  • 409
  • 1
  • 5
  • 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
  • 17,669
  • 10
  • 51
  • 89
  • 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
  • 859
  • 1
  • 17
  • 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
  • 178,883
  • 35
  • 278
  • 309
-2

Just use python's replace function:

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

Output should look like this:

>> kick
>> ban
TabeaKischka
  • 793
  • 6
  • 15
  • 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