[Tutor] [tutor] Difference between index and find in manipulating strings
Hello, i have again very basic question in python string management. I am not able to understand how find and index works and what do they actually return. line = this is varsha print line.find(is) 2 print line.rfind(is) 5 print line.rfind(varsha) 8 print line.index(varsha) 8 what does 2 in first line signifies... and why rfind gave 5 as an output... can anybody pls explain me what exactly is interpreter tryin to return. -- Varsha Purohit, ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [tutor] Difference between index and find in manipulating strings
Varsha Purohit wrote: Hello, i have again very basic question in python string management. I am not able to understand how find and index works and what do they actually return. line = this is varsha print line.find(is) 2 print line.rfind(is) 5 print line.rfind(varsha) 8 print line.index(varsha) 8 what does 2 in first line signifies... and why rfind gave 5 as an output... can anybody pls explain me what exactly is interpreter tryin to return. Sure. The 2 in first line means the string 'is' begins in line[2] that would be the 'is' in 'this' (remember line's first character 't' is line[0]). OTOH rfind looks for the first appearance of 'is' in line but starting from the right, that would be the word 'is' which starts at line[5]. As there is only one occurrence of 'varsha' in line both methods, find and rfind will give the same answer : 8. From Python 2.5 documentation : index( sub[, start[, end]]) Like find(), but raise ValueError when the substring is not found. HTH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [tutor] Difference between index and find in manipulating strings
Thanks guys it was really helpful i m just a beginner in python start to program since two days back. So finding little difficulty with some concepts. On 9/2/07, Ricardo Aráoz [EMAIL PROTECTED] wrote: Varsha Purohit wrote: Hello, i have again very basic question in python string management. I am not able to understand how find and index works and what do they actually return. line = this is varsha print line.find(is) 2 print line.rfind(is) 5 print line.rfind(varsha) 8 print line.index(varsha) 8 what does 2 in first line signifies... and why rfind gave 5 as an output... can anybody pls explain me what exactly is interpreter tryin to return. Sure. The 2 in first line means the string 'is' begins in line[2] that would be the 'is' in 'this' (remember line's first character 't' is line[0]). OTOH rfind looks for the first appearance of 'is' in line but starting from the right, that would be the word 'is' which starts at line[5]. As there is only one occurrence of 'varsha' in line both methods, find and rfind will give the same answer : 8. From Python 2.5 documentation : index( sub[, start[, end]]) Like find(), but raise ValueError when the substring is not found. HTH -- Varsha Purohit, Graduate Student, San Diego State University ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [tutor] Difference between index and find in manipulating strings
Ricardo Aráoz [EMAIL PROTECTED] wrote From Python 2.5 documentation : index( sub[, start[, end]]) Like find(), but raise ValueError when the substring is not found. As opposed to find() which returns -1 when the string is not found. That means you can use try/except with index but must check for -1 with find: if 'foo'.find('q') == -1: print 'oops!' as opposed to try: 'foo'.index('q') except ValueErrror: print 'oops!' Which style is best will depend on what you are doing. HTH, Alan g ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [tutor] Difference between index and find in manipulating strings
Varsha Purohit wrote: Thanks guys it was really helpful i m just a beginner in python start to program since two days back. So finding little difficulty with some concepts. Don't worry, I'm a 20 days old timer. ;c) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [tutor] Difference between index and find in manipulating strings
Ricardo Aráoz [EMAIL PROTECTED] wrote Thanks guys it was really helpful i m just a beginner in python start to program since two days back. So finding little difficulty with some concepts. Don't worry, I'm a 20 days old timer. ;c) And I've been using Python for over 10 years now and still manage to pick up at least one new idea every week on this list! :-) -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor