2

For instance, if my initial string is "hello world! how are you? 0" I would like for the resulting string to be "hlo ol! hw r yu?". So far I have the following code:

s = "hello world! how are you? 0"  
for char in s:
    if char.isalpha() == True:

5 Answers5

2
i = 0
s2 = ""
for char in s:
    if char.isalpha() or char.isnumeric():
        if (i % 2) == 0:
            s2 += char
        i += 1 
    else:
        s2 += char

the output string s2 will be:

# s2 = 'hlo ol! hw r yu? '
Itay
  • 16,601
  • 2
  • 51
  • 72
0

Try this:

>>> s = "hello world! how are you? 0"
>>> ' '.join(j[::2] if i%2==0 else j[1::2] for i,j in enumerate(''.join(k for k in s if k.isalpha() or k==' ').split()))
'hlo ol hw r yu'

First we remove all non-alphabetical characters and spaces with ''.join(k for k in s if k.isalpha() or k==' '). This produces 'hello world how are you '. Then we split it. We get ['hello', 'world', 'how', 'are', 'you']. Now For each item in this list we skip alternating characters in the string starting from second index if they in odd position (index) and skip alternating characters in the string from the first index if they are in even position (index).

This is equivalent to :

s1 = ''.join(k for k in s if k.isalpha() or k==' ') #'hello world how are you'
s1_list = s1.split() #['hello', 'world', 'how', 'are', 'you']
s2_list = [j[::2] if i%2==0 else j[1::2] for i,j in enumerate(s1_list)] #['hlo', 'ol', 'hw', 'r', 'yu']
s3 = ' '.join(s2_list) #'hlo ol hw r yu'
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
0

If you Don't 100% need to use the char.isalpha()

s = "hello world! how are you? 0"

i = 0  
for char in s:
    if char == " " or (i % 2) != 0:
        s2 += char
    i += 1

s = s2

if you wanted every odd character rather than the even ones, simple remove the "not" from the if statement to reverse the logic

Or amend the above by taking out the if statement and inserting outside of the client code

char.isalpha():
    if char == " " or (i % 2) != 0:
        s2 += char

s = "hello world! how are you? 0"

i = 0  
for char in s:
    char.isalpha()

s = s2

Personally I would opt for the top because it's less confusing, particularly if you don't need to use the function elsewhere

Instream
  • 1
  • 1
0

Ok, just playing around.. I like the c[::2] operator method. Problem here is that the count starts with the first letter in the word and doesn't include spaces.. but it was fun.

import re
import string

s = "hello world! how are you? 0"  

split_by_punc = re.findall(f"[\w]+|[{string.punctuation}]", s)

result = ' '.join(c[::2] if c[::2].isalnum() else c for c in split_by_punc)
for punc in string.punctuation:
    result = result.replace(f' {punc}', punc)  # remove extra spaces before punctuation

"hlo wrd! hw ae yu? 0"

monkut
  • 42,176
  • 24
  • 124
  • 155
0

You have used isalpha() , instead use isalnum() to include both the alphabets and the numeric values .

word = "hello world! how are you? 0"
index = 0 
result = ""
for letter in word:
    if(letter.isalnum() == False):
        result += letter
    elif(index == 0 and letter.isalnum() == True):
        result += letter
        index = 1
    else:
        index = 0
print (result)
Syed Jafer
  • 310
  • 3
  • 13