Diamond
Diamond

Reputation: 31

Printing odd numbered characters in a string without string slicing?

I'm currently doing a project for my university, and one of the assignments was to get python to only print the odd characters in a string, when I looked this up all I could find were string slicing solutions which I was told not to use to complete this task. I was also told to use a loop for this as well. Please help, thank you in advance. Here is my code so far, it prints the string in each individual character using a for loop, and I need to modify it so that it prints the odd characters.

i = input("Please insert characters: ")
for character in i:
    print(character)

Upvotes: 3

Views: 26494

Answers (8)

Elnaz
Elnaz

Reputation: 111

You can use gapped index calling. s[start:end:gap]

s = 'abcdefgh'
print(s[::2])
# 'aceg'

returning the characters with indexes 0, 2, 4, and 6. It starts from position 0 to the end and continues with a gap of 2.

print(s[1::2])
# 'bdfh'

Returning the characters with indexes 1, 3, 5, and 7. It starts from position 1 and goes with a gap of 2.

Upvotes: 0

Lais Lodi
Lais Lodi

Reputation: 11

When we want to split a string, we can use the syntax:

str[beg:end:jump]

When we put just one number, this number indicates the index. When we put just two numbers, the first indicates the first character (included) and the second, the last character (excluded) of the substring

You can just read the string and print it like this:

i = input("Please insert characters: ")
print(i[::2])

When you put str[::] the return is all the string (from 0 to len(str)), the last number means the jump you want to take, that meaning that you will print the characters 0, 2, 4, etc

Upvotes: 0

Rishi Anand
Rishi Anand

Reputation: 1

Try this code. For even index start you can use range(0, len(s), 2).

s = input()
res = ''
for i in range(1,len(s),2):
    res +=s[i]
print(res)

Upvotes: 0

Devi Ojha
Devi Ojha

Reputation: 201

s = raw_input()
even_string=''
odd_string=''
for index in range(len(s)):
    if index % 2 != 0:
        odd_string = odd_string+s[index]
    else:
        even_string = even_string+ (s[index])
print even_string,odd_string

Upvotes: 0

Nithin R
Nithin R

Reputation: 949

Please follow this code to print odd numbered characters

#Read Input String 
i = input("Please insert characters: ")
#Index through the entire string
for index in range(len(i)):
    #Check if odd 
    if(index % 2 != 0):
        #print odd characters
        print(i[index])

Upvotes: 1

muhammad hamizee
muhammad hamizee

Reputation: 1

person = raw_input("Enter your name: ") a = 1 print"hello " , person print "total : " , len(person) for each in list (person): if a % 2 ==0: print "even chars : " , (each) a+=1 else: a+=1

Upvotes: 0

Michael T
Michael T

Reputation: 1043

I think more information would be helpful to answer this question. It is not clear to me whether the string only contains numbers. Anyway, maybe this answers your question.

string = '38566593'

for num in string:
    if int(num) % 2 == 1:
        print(num)

To extend on Kyrubas's answer, you could use-

string = '38566593'

for i, char in enumerate(string):
    if i % 2 == 1:
        print(char)

Upvotes: 0

Kyrubas
Kyrubas

Reputation: 897

Another option:

a= 'this is my code'
count = 1
for each in list(a):
    if count % 2 != 0:
        print(each)
        count+=1
    else:
        count+=1

Upvotes: 0

Related Questions