Home > Backend Development > Python Tutorial > How to count the number of letters in a string using python?

How to count the number of letters in a string using python?

coldplay.xixi
Release: 2020-06-09 15:17:58
Original
15065 people have browsed it

How to count the number of letters in a string using python?

How to count the number of letters in a string using python?

Python’s method of counting the number of letters in a string:

Use these three codes first

dic=dict()
d={}
s=set()
s='helloworld'(1)d=dict()    for x in s:
      if x not in d.keys():
          d[x]=1       else:
         d[x]=d[x]+1    print(d)
(2)d2=dict()    for x in s:
      d2[x]=d2.get(x,0)+1    print(d2)
(3)d3=dict()
   for x in s:
      d3[x]=s.count(x)
   print(d3)
Copy after login

These three methods are all It is output in the form of a dictionary, but it can be seen that it is easier to use the second and third built-in function methods.

def countchar(str):
    str=str.lower()#化成小写
    ans=[]    for i in range(26): #列表赋初值  26 个 0
        ans.append(0)    for i in str:        if(ord(i)>=ord(&#39;a&#39;)and ord(i)<=ord(&#39;z&#39;)):
            ans[ord(i)-ord(&#39;a&#39;)]=ans[ord(i)-ord(&#39;a&#39;)]+1  #统计个数
    return ansif __name__ == "__main__":
     str = input()     print(countchar(str))def countchar(st):                        #定义数个数的函数    
    keys = [chr(i+97) for i in range(26)] #生成26个字母的key列表    
    di = dict().fromkeys(keys,0)          #赋给每个key初始值0    
    new = []                  #建立一个新列表用于存放有序的key    
    st = st.lower()           #将所有输入的字符改为小写
    
    for s in st:              #遍历字符串   
            di[s] = st.count(s) #输出每个字符的个数,存放到字典里
        for k in keys:        #遍历keys,将其在di中的值添加到新列表,获得有序的26个字母的个数
            new.append(di[k])        return new            #返回存有26个字母个数的列表if __name__ == "__main__":
    st = input()              #输入字符串
    str1 = ""                 #定义一个空字符串
    for s in st:              #遍历输入的字符串
        if s.isalpha() != 0:  #只有字母才添加到新字符串,标点忽略不计  
            str1 += s    
    print(countchar(str1))    #输出列表
Copy after login

The above two methods also output the number of occurrences of letters in the string. The slight difference is that here it first sets 26 letters and makes the corresponding initial value 0. Then count the number of occurrences of each letter in the string, and how many times each letter appears is the value at its corresponding initial value. The corresponding value of the letters that do not appear is still the initial value 0

Through the above methods, it is not difficult for us to summarize the idea of ​​​​solving this problem: randomly input a string from the keyboard, and then Loop through the string, count the number of occurrences of various characters by looping through each character in the string, and loop through the string.

Recommended tutorial: "Python Video Tutorial"

The above is the detailed content of How to count the number of letters in a string using python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template