Is there a way to use map() function with a string instead of a list? Or the map() function is meant only to work with lists?
For instance, ignoring the content of the lambda function, this code returns a map object and not a string:
def rot_13(string):
alph = 'abcdefghijklmnopqrstuwxyz'
return str(map(lambda i: alph[(alph.find(i)+13) % len(alph)], string))
Using list/generator comprehensions is preferable (more Pythonic) to map():
def rot_13(string):
alph = 'abcdefghijklmnopqrstuwxyz'
return ''.join(alph[(alph.find(i)+13) % len(alph)] for i in string)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With