The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Python code for a string program
Discuss Python code for a string program in the Python Programming forum on Dev Shed. Python code for a string program Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 8th, 2012, 10:10 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 3
Time spent in forums: 10 m 29 sec
Reputation Power: 0
|
|
|
Python code for a string program
HI!
could anyone help me out with the code for below question:
Given two strings, return True if either of the strings appears at the very end of the other string, ignoring upper/lower case differences (in other words, the computation should not be "case sensitive"). Note: s.lower() returns the lowercase version of a string.
end_other('Hiabc', 'abc') → True
end_other('AbC', 'HiaBc') → True
end_other('abc', 'abXabc') → True
|

December 8th, 2012, 04:53 PM
|
 |
Contributing User
|
|
|
|
Code:
>>> import pprint
>>> pprint . pprint ( dir ( '' ) )
['__add__',
'__class__',
...
'endswith',
...
'zfill']
>>>
__________________
[code] Code tags[/code] are essential for python code!
|

December 9th, 2012, 12:25 PM
|
|
Contributing User
|
|
Join Date: May 2009
Posts: 297
  
Time spent in forums: 3 Days 19 h 28 m 48 sec
Reputation Power: 7
|
|
|
You can either reverse the letters in the words and check the beginning or convert to lists and start at -1 comparing letters.
|

December 9th, 2012, 03:01 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
Code:
def end_other(x, y):
return x[::-1][0:len(y)] == y[::-1]
case-insensitive:
Code:
def end_other(x, y):
return (x[::-1][0:len(y)]).lower() == (y[::-1]).lower()
Last edited by AndrewSW : December 9th, 2012 at 03:04 PM.
|

December 9th, 2012, 03:06 PM
|
 |
Contributing User
|
|
|
|
|
You don't seem to get it.
endswith is a string method.
|

December 9th, 2012, 03:19 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
Quote: | Originally Posted by b49P23TIvg You don't seem to get it.
endswith is a string method. |
I suppose if this is homework they might not be expected to use a function, other than lower().. maybe len(). Although, my method is a bit cheeky 
|

December 9th, 2012, 03:23 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
Quote: | Originally Posted by dwblas You can either reverse the letters in the words and check the beginning or convert to lists and start at -1 comparing letters. |
Converting to lists is unnecessary as a string is already an iterable.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|