Ad
Fundamentals
Strings

when you are doing just one line operation and you're not adding any doc string to your function using lambda functions will remove some unnecessary lines

Code
Diff
  • reverse_string = lambda s: s[::-1]
    
    # Example usage
    print(reverse_string("Hello, World!"))  # "!dlroW, Hello"
    print(reverse_string(""))  # ""
    print(reverse_string("a"))  # "a"
    print(reverse_string("ab"))  # "ba"
    print(reverse_string("abc"))  # "cba"
    • def reverse_string(string):
    • return string[::-1]
    • reverse_string = lambda s: s[::-1]
    • # Example usage
    • print(reverse_string("Hello, World!")) # "!dlroW, Hello"
    • print(reverse_string("")) # ""
    • print(reverse_string("a")) # "a"
    • print(reverse_string("ab")) # "ba"
    • print(reverse_string("abc")) # "cba"