简体   繁体   中英

how to count characters in a string without the white spaces using python?

I created a function that would count the number of characters in a string but it also counts the white spaces.

Here is my code:

def count_characters_in_string(mystring):
    string_1 = mystring
    print("The number of characters in this string is:", len(string_1))
count_characters_in_string("Apples and Bananas")

Is there a way to not count the white spaces?

This one does not create a spurious list or str in memory, using sum , str.isspace , and the fact that issubclass(bool, int) :

def count_characters_in_string(mystring):
    return sum(not c.isspace() for c in mystring)

You can split the string (not specifying the separator means split according to any whitespace) and then sum up the lenghts of the resulting strings

def count_characters_in_string(string):
    return sum([len(word) for word in string.split()])

print("The number of characters in this string is:", count_characters_in_string("Apples and Bananas"))

EDIT : following the useful advice from @schwobaseggl, I created 3 versions of the function and timed them to find out the map option is actually the fastest

def count_characters_in_string(string):
    return sum([len(word) for word in string.split()])

def count_characters_in_string_gen(string):
    return sum(len(word) for word in string.split())

def count_characters_in_string_map(string):
    return sum(map(len, string.split()))


%timeit count_characters_in_string("Apples and Bananas")
718 ns ± 21.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit count_characters_in_string_gen("Apples and Bananas")
812 ns ± 13.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit count_characters_in_string_map("Apples and Bananas")
607 ns ± 19.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Use this:

def count_characters_in_string(input_string):

    letter_count = 0

    for char in input_string:
        if char.isalpha():
            letter_count += 1

    print("The number of characters in this string is:", letter_count)

This works as well:

def count_characters_in_string(input_string):

    letter_count = len(input_string.replace(" ",""))

    print("The number of characters in this string is:", letter_count)

When you then run:

count_characters_in_string("Apple Banana")

It'll output:

"The number of characters in this string is: 11"

well you can do a simple thing, create a new variable with the same string, replace all the white spaces on that string for nothing, after that you get the length of the both strings and get the difference.

string1 = "asd asd asd asd asd"
string2 = string1
string2.replace(" ", "")
size1 = len(string1)
size2 = len(strgin2)
size3 = size1 - size2

cheers

What do you think about len of list comprehension ?

def count_characters_in_string(mystring):
    return len([character for character in mystring if character.isalpha()])

one of the solution could be to iterate through the characters and check if it not space then increment the count as below:

def count_characters_in_string(mystring):
    count = 0
    for x in mystring:
        if x !=' ':
            count+=1
    return count

res = count_characters_in_string("Apples and Bananas")
print(res)

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