How to use the .isalpha() or a similar method here? | SoloLearn: Learn to code for FREE!

+2

# How to use the .isalpha() or a similar method here?

I'm trying to find a way to return only the letters of an input string, but my code is returning the (' ') count as well, I don't want that, I know that there is a method .isalpha() that only returns letters as far as I know, however I'm not sure to make it work here, thanks in advance https://code.sololearn.com/chv9Lb361CAr/?ref=app

7/11/2020 3:55:03 AM

Whally O

+4

Is alpha() is ok, but you can also check if a given alphabet is within a given ascii value's range.. In this code, I used (97, 123) => (a, z). Another method is to use the regular expression https://code.sololearn.com/cDYtPAfEuytJ/?ref=app

+4

AKSHAY Everyone will understand my answer, I'm not sure of the code. Answer is not a code, answer is the content of my comment without the code

+3

Some ways are here 👇 def count_letters(t): text="" for i in t: if i.isalpha(): text+=i result = {} text = text.lower() for letter in text: if letter not in result: result[letter] = 0 result[letter] += 1 return result print(count_letters("AaB bCc")) def count_letters(t): text="" for i in t: if i !=" ": text+=i result = {} text = text.lower() for letter in text: if letter not in result: result[letter] = 0 result[letter] += 1 return result print(count_letters("Aa BbCc")) def count_letters(text): text=text.replace(" ","") result = {} text = text.lower() for letter in text: if letter not in result: result[letter] = 0 result[letter] += 1 return result print(count_letters("Aa BbCc")) Many ways to remove spaces from a string is here 👇 https://www.geeksforgeeks.org/python-remove-spaces-from-a-string/

+3

Whally O 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 the code I shared is just a piece of code I have in my snippets for input validation. I am not suggesting you use a while loop to replace your for loop, I was showing you isalpha() and giving you some working code to try and see if isalpha() will work for you. 😉👍 The code basically will keep asking for input unless ONLY alphabetical characters (no numerals, spaces, or symbols allowed) are input and then it will break out and print the result. Happy Coding!

+3

Whally O just return the dictionary named "result" instead of "string". You're all done 👍🏻👍🏻

+2

Whally O All are working , check again https://code.sololearn.com/cgYTyG7UCs5V/?ref=app

+2

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 is there right now, I modified the var: text=text.replace(" ", "") as you taught me

+2

Code Crasher great! Thank you very much, don't get me wrong, any assistance or help is more than welcome!, and now I can a find a way with a while loop because of you

+2

Mirielle [INACTIVE] your answer is good but for beginners it may be hard to understand. So you should give easy answer(edit: code) so that the person who is asking can understand it and use that logic..

+2

Mirielle [INACTIVE] ok sorry for 'answer' instead of 'code'. I am talking about the code('one line' for beginner is not good habit) But why to use ASCII if python alreday has "isalpha" method.

+1

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Are you using the parameter (t) as test? In your first example is not necessary to indent after the for loop? Sorry, but is showing me error on the first and third example, I'll try with the second.

+1

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Yes, they are working, I'm trying to work with the third one, but is returning some other characters like '=', '+'

+1

Whally O Show me that code

+1

# Take a look at this code, it might help: while True: inp = input() if inp.isalpha(): break print('input = ', inp)

+1

Code Crasher you should also explain the code

+1

Whally O Yeah , now it will work

+1

Code Crasher Thanks, the thing is that the for loop is part of the problem steps, built-in

+1

Mirielle [INACTIVE] Thank you! Now I can try a way with "lambda" as well

+1

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 What's up? I hope everything is fine with you, not only spaces, basically they only want the keys to return a count of the values of letters only, sorry if I sound repetitive, I'm also trying to find different ways with the problem requirements, that website that you mentioned is amazing, I use their knowledge and resources sometimes

+1

Whally O check this.. (I think it can solve your problem..) edit: improved printing of number of letters.. https://code.sololearn.com/cWTJFnh2V98p/?ref=app