【问题标题】:Counting the special character and character in an array and giving the sum of the numbers in an array计算数组中的特殊字符和字符并给出数组中数字的总和
【发布时间】:2022-11-03 00:26:44
【问题描述】:
ar=input("Enter the elements:").split()
sum=0
cr=0

for i in range(len(ar)):
    if(not ar[i].isalpha()):
        a=int(ar[i])
        sum=sum+a
    else:
         cr=cr+1
print("Sum of the elements is :",sum)
print(" No of Characters in the array:",cr)

        

我可以计算字符,但不知道如何处理特殊字符

计算特殊字符并给出总和

【问题讨论】:

  • 您能否分享示例输入和预期输出。你能解释一下“特殊字符”是什么意思吗?

标签: python arrays special-characters


【解决方案1】:

你检查它是否既不是alpha niether numeric

ar=input("Enter the elements:")
char_count = 0
special_char_count = 0

for char in ar:
    if char.isalpha():
        char_count += 1
        continue
    
    if not char.isnumeric():
        special_char_count += 1
    
print("Sum of alphabet: ", char_count)
print("Sum of special charachters: ", special_char_count)

# Input: abc&--
# Output: Sum of alphabet:  3
#         Sum of special charachters:  2

【讨论】:

    猜你喜欢
    • 2014-02-16
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 2020-06-14
    相关资源
    最近更新 更多