0
string = "Northeastern Africa"
myString = string.lower()
index = 0
num_index = int(index)

length = len(myString)
num_length = int(length)
num_length = num_length - 1

while myString[num_index] >=  myString[18]:
    print(num_index)
    print(num_length)
    print(myString[num_index])
    print(num_index)
    num_index = num_index +1
    print(myString[0:num_index])
    print(" ")

why does it only print "northeastern" and stops at the next space? how do i make it go through the full string without stopping at the space in between both words?

6
  • What is myString here? Commented Jan 27, 2015 at 8:24
  • index and length are already integers. There is no need to convert them to integers again. Commented Jan 27, 2015 at 8:25
  • Your code won't work the way you state if your string really starts with a capital N. Are you sure it doesn't start with a n (lowercase)? Commented Jan 27, 2015 at 8:27
  • @MartijnPieters Have you figured out what he's trying to do in the first place? Commented Jan 27, 2015 at 8:27
  • oh sorry, myString = string.lower() Commented Jan 27, 2015 at 8:28

2 Answers 2

1

Your while loop compares each character with the last, a, stopping when it finds a character that isn't equal or higher than a. Your code stops at the space because the latter's position in the ASCII table is 32:

>>> ' ' < 'a'
True
>>> ord(' ')
32
>>> ord('a')
97

You probably wanted to create a loop comparing num_index to num_length instead:

while num_index <= num_length:

If you wanted to loop through all characters in a string, just use a for loop:

for character in myString:
    print(character)

You can use the enumerate() function to add an index:

for index, character in enumerate(myString):
    print(index, character, sep=': ')
    print(myString[:index])
Sign up to request clarification or add additional context in comments.

2 Comments

ok, what im trying to do is to create a loop that counts the vowels in the string. that's why i wanted to go down the string letter by letter.
@user3294540: then just use the simple for loop and count your vowels! if character in 'aeiou':.
0

because myString[num_index] value is space check this..

>>> " ">="a"
False

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.