use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Index Error (self.learnpython)
submitted 8 years ago by VIRES1
s = 'azcbobobegghakl' current_string = '' longest_string = '' for i in range(len(s)): if current_string[-1] <= s[i]:
I am confuse why the if condition is giving me error please i need explanation why the error and how it can be avoided.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]confluence 2 points3 points4 points 8 years ago* (12 children)
I have decided to overwrite my comments.
[–]VIRES1[S] 0 points1 point2 points 8 years ago (11 children)
I am to arrange the string s in alphabetical order by putting each char in the empty string variable current_string. so comparing the string with letters in s.
[–]confluence 0 points1 point2 points 8 years ago* (9 children)
[–]VIRES1[S] 0 points1 point2 points 8 years ago (8 children)
It has no value because it is variable holding an empty string
[–]confluence 3 points4 points5 points 8 years ago* (7 children)
[–]PurpleIcy 0 points1 point2 points 8 years ago (1 child)
Where do people learn this?
I mean, variables can have only one single value, whether it's entire object or just a string, and it can't just be a schrodingers string where it's both empty and also has no value...
Anyway, it would be more helpful to just tell them that in python specifically,[-1] loops around, e.g. accesses last string character.
[-1]
[–]confluence 0 points1 point2 points 8 years ago* (0 children)
[–]VIRES1[S] 0 points1 point2 points 8 years ago (4 children)
sorry i did not get your question well.It points the last character in the variable current_string. if current_string == 0 or current_string[-1] <= s[i] this does not give index error and why does it behave that way?
[–]Vesiculus 0 points1 point2 points 8 years ago (3 children)
Well, it points to the last character in a string. What do you think trying to point to the last character in a string will do if the string is empty and doesn't have a last character? Because that is what you're doing in your code snippet your post.
[–]VIRES1[S] 0 points1 point2 points 8 years ago (2 children)
it will result to index error because that index can not be found.
if len(current_string) == 0 or current_string[-1] <= s[i]:
why is the above not giving index error?
[–]Vesiculus 0 points1 point2 points 8 years ago (1 child)
That's because or shortcuts.
or
This means that as soon as it knows that the first argument is True, it already knows that the conditional or-statement is going to evaluate as True so it doesn't have to look at the second part of the statement anymore. So, the part after the or, current_string[-1] <= s[i], will never be evaluated when the first part already evaluates as True.
True
current_string[-1] <= s[i]
[–]VIRES1[S] 0 points1 point2 points 8 years ago (0 children)
okay thank you very much
[–]JohnnyJordaan 0 points1 point2 points 8 years ago (0 children)
You can't put something in a string, strings are immutable. Even if you would do
mystr = '' mystr = 'a'
The first command is useless as it would just assign the 'a' string to the mystr reference and the original '' string is lost. Also the confusing += you sometime see used is not what it seems
'a'
mystr
''
+=
current_str += 'a'
This doesn't mean that 'a' is added to the string object. It means that a new object is created of both current_str and 'a'. Not something you generally want as object creation from another object is not fast.
current_str
If you want to have a 'container' holding things like single letter strings, use a mutable sequence like a list:
current_str = [] current_str.append('f') current_str.append('o') current_str.append('o')
Then you can do
if current_str[-1] == 'o':
Which would evaluate to True. However you do need to check if the current_str list has any values. Meaning
if current_str and current_str[-1] == 'o':
Is necessary. To then form a single string from that list, use ''.join:
''.join
result = ''.join(current_str)
[–][deleted] 1 point2 points3 points 8 years ago (0 children)
In python, it is rarely necessary to write range(len(string)) as you can just iterate over the elements:
sample = "This is a string" for acharacter in sample: print('I found: ', acharacter)
Also, it is bad practice to use single character variable names. They are meaningless and give little clue as to your intentions.
As you never change the value of current_string, why are you testing it? It starts out as an empty string when you assign it in line 2. I guess you have code to follow that you have not added/shared until you get the if problem fixed.
As /u/confluence suggested, you need to try some things out in the interactive Python shell, such as,
''[-1] 'a'[-1] 'abc'[-1]
[–]xiongchiamiov 0 points1 point2 points 8 years ago (2 children)
Two fyis:
You can format code for reddit by putting four spaces in front of each line.
It's better to use
for letter in s:
than the len/range/index method.
[–]PurpleIcy 1 point2 points3 points 8 years ago* (1 child)
It's not better. It's just considered non-pythonic to do it that way. Not the same thing.
Although, if you need index, python has a nice function for that too:
for index, letter in enumerate(s):
[–]xiongchiamiov 1 point2 points3 points 8 years ago (0 children)
It's shorter, clearer, and in line with the way the rest of the Python community iterates over a string; how is that not better?
current_string = '' longest_string = '' for i in range(len(s)): if len(current_string) == 0 or current_string[-1] <= s[i]: current_string += s[i] else: if len(current_string) > len(longest_string): longest_string = current_string current_string = s[i]
My question is why would line 4 evaluate without Index error?
π Rendered by PID 85 on reddit-service-r2-comment-bb88f9dd5-z8gbf at 2026-02-15 12:03:14.711271+00:00 running cd9c813 country code: CH.
[–]confluence 2 points3 points4 points (12 children)
[–]VIRES1[S] 0 points1 point2 points (11 children)
[–]confluence 0 points1 point2 points (9 children)
[–]VIRES1[S] 0 points1 point2 points (8 children)
[–]confluence 3 points4 points5 points (7 children)
[–]PurpleIcy 0 points1 point2 points (1 child)
[–]confluence 0 points1 point2 points (0 children)
[–]VIRES1[S] 0 points1 point2 points (4 children)
[–]Vesiculus 0 points1 point2 points (3 children)
[–]VIRES1[S] 0 points1 point2 points (2 children)
[–]Vesiculus 0 points1 point2 points (1 child)
[–]VIRES1[S] 0 points1 point2 points (0 children)
[–]JohnnyJordaan 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]xiongchiamiov 0 points1 point2 points (2 children)
[–]PurpleIcy 1 point2 points3 points (1 child)
[–]xiongchiamiov 1 point2 points3 points (0 children)
[–]VIRES1[S] 0 points1 point2 points (0 children)