Top Python tricks for efficient programmingStrings
Trick 6: Reversing the stringa=”python”
print(“Reverse is”, a[::-1])
Output:
Reverse is nohtyp
Trick 7: Splitting the stringa="Python is the language of the future"
b=a.split()
print(b)
Output:
[‘Python’, ‘is’, ‘the’, ‘language’, ‘of’, 'the’, ‘future’]
Trick 8: Printing out multiple values of stringsprint(“on”*3+’ ‘+”off”*2)
Output:
ononon offoff
Trick 9: Creating a single stringa = [“I”, “am”, “not”, “available”]
print(“ “.join(a))
Output:
I am not available
Trick 10: Checking if two words are anagramsfrom collections import Counter
def is_anagram(str1, str2):
return Counter(str1) == Counter(str2)
print(is_anagram(‘taste’, ‘state))
print(is_anagram(‘beach’, ‘peach’))
Output:
True
False