[英]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)
:这个不会使用
sum
、 str.isspace
以及issubclass(bool, int)
的事实在内存中创建虚假list
或str
:
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您可以
split
字符串(不指定分隔符意味着根据任何空格拆分),然后总结结果字符串的长度
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编辑:按照@schwobaseggl 的有用建议,我创建了 3 个版本的函数并定时它们以找出
map
选项实际上是最快的
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
?您如何看待
list comprehension
len
?
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.