I am trying to write a python code to check if two strings are anagrams of each other or not :
s1= input("enter 1st word or phrase: ")
s2= input("Enter your 2nd word or phrase: ")
s1=s1.lower()
s2=s2.lower()
if len(s1) != len(s2):
print("Not Anagrams")
else:
for x in s1:
if s1.count(x) != s2.count(x):
print("Not Anagrams")
break
else:
print("Anagrams")
This code is working fine for now , for example for strings like : listen ; silent or medical ;decimal or State;taSte
but it does not work for strings like : snooze alarms ; alas, no more Z's
Could you help me fix this code so that it also works for scenarios like this ?
Hi, @Chetan_Tiwary_
Well, I believe the strings snooze alarms' and alas, no more Z's are not considered anagrams by your Python code, because the second string has a comma after the word "alas" that the second string doesn't have. Do you want to completely ignore any punctuation character (single quote, double quotes, commas, spaces, etc...) in your anagram verification?
@ric yes , that will serve my purpose.
Thanks for the clarification, @Chetan_Tiwary_
Here's a rewritten version of your Python code that ignores non-alphabetic characters (with the help of the web site https://tohtml.com to be able to maintain correct indentation and formatting of the code in this reply of mine):
s1 = input("enter 1st word or phrase: ") s2 = input("Enter your 2nd word or phrase: ") s1 = s1.lower() s2 = s2.lower() s1 = ''.join([char for char in s1 if char.isalpha()]) s2 = ''.join([char for char in s2 if char.isalpha()]) if len(s1) != len(s2): print("Not Anagrams") else: for x in s1: if not(x.isalpha()): continue if ( (s1.count(x) != s2.count(x)) ): print("Not Anagrams") break else: print("Anagrams")
And here's the execution of the modified Python program for the several examples that you used:
$ python3 anagrams2.py
enter 1st word or phrase: listen
Enter your 2nd word or phrase: silent
Anagrams
$ python3 anagrams2.py
enter 1st word or phrase: medical
Enter your 2nd word or phrase: decimal
Anagrams
$ python3 anagrams2.py
enter 1st word or phrase: State
Enter your 2nd word or phrase: taSte
Anagrams
$ python3 anagrams2.py
enter 1st word or phrase: snooze alarms
Enter your 2nd word or phrase: alas, no more Z's
Anagrams
wonderful @ric , this
''.join([char for char in s1 if char.isalpha()])
efficiently strips any character that isn't a letter, giving you a clean, alphabet-only string !
kudos!
Sorry if I hijack the thread, how to do you type in code like that ?
The toolbow I see just contains: bold, italic, underline, insert/edit link, insert photo and numbered list.
@TM while replying , do you see an option insert code </> ? You can use that.
No, I do not see it.
Just the 6 I mentionned earlier.
If it matters, I have been lazy and I am still using a Fedora 36 laptop withMozilla Firefox 112.0.1 !
Hi, @Chetan_Tiwary_
I belleve that @TM is right: as far as I can see, we (common users / members) only get the expanded / full toolbar, in the post / message editor, when we start a new discussion.
1 - Screenshot for when we start a new discussion (we get the full toolbar):
2 - Screenshot for when we reply to an existing discussion (we get a toolbar that has only the icons / buttons for Bold, Italic, Underline, Create Hyperlink, Insert Image and Insert Bulleted List):
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.