简体   繁体   中英

How to check if there are characters in a string?

I will rephrase my queston cause I wasn't really clear sorry. [enter code here][1]

Here is my code , before that I have a list called sentence that the user inputs with stuff he wants [1]: https://i.stack.imgur.com/1v7I5.png

The thing is that the variable mySentence still contains , , , if you enter , , , , , , in it. I don't understand why.

Sorry I wasn't clear enough

With regexp maybe :

import re
strg = "0129872198a28379"
reg = re.compile("[A-Za-z]+")
test = reg.match(strg)

One possible solution might be:

def contains_char(str)
    for c in str:
        if ord(c.lower()) >= 97 and ord(c.lower()) <= 122:
            return True
    return False

The function is checking whether the ASCII value of the character is between that for letters of the English alphabet ( ord() returns the ASCII value).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM