python 字符串练习

1. 输入一行字符,统计其中有多少个单词,每两个单词之间以空格隔开。如输入: This is a c++ program. 输出:There are 5 words in the line.  【考核知识点:字符串操作】

a = input()
b = a.split(' ')
num = len(b)
print("There are %d words in the line" %(num))


2. 给出一个字符串,在程序中赋初值为一个句子,例如"he threw three free throws",自编函数完成下面的功能:

    1)求出字符列表中字符的个数(对于例句,输出为26);

a = input()
num = len(a)
print("There are %d words in the line" %(num))


    
    2)计算句子中各字符出现的频数(通过字典存储); ---学完字典再实现

from collections import defaultdict
a = input()
s = defaultdict(int)
for c in a:
    s[c] += 1
print(s)

  

 3) 将统计的信息存储到文件《统计.txt》中;    --- 学完文件操作再实现
 


4. # (2017-好未来-笔试编程题)--练习

- 题目描述:
输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”

- 输入描述:
每个测试输入包含2个字符串

- 输出描述:
输出删除后的字符串

- 示例1:

```
输入
    They are students.
    aeiou
输出
    Thy r stdnts.
```

a = list(input())
b = list(input())
for i in a:
    if i in b:
        a.remove(i)
print(''.join(a))


5. # (2017-网易-笔试编程题)-字符串练习

小易喜欢的单词具有以下特性:
    1.单词每个字母都是大写字母
    2.单词没有连续相等的字母
列可能不连续。
例如:
    小易不喜欢"ABBA",因为这里有两个连续的'B'
    小易喜欢"A","ABA"和"ABCBA"这些单词
    给你一个单词,你要回答小易是否会喜欢这个单词。

- 输入描述:
输入为一个字符串,都由大写字母组成,长度小于100

- 输出描述:
如果小易喜欢输出"Likes",不喜欢输出"Dislikes"


示例1 :

```
输入
    AAA
输出
    Dislikes

```

a = input()
b = len(a)
for i in range(1,b):
    if not (a[i].isupper() and a[i-1] != a[i] and a[0].isupper()):
        print("Dislikes")
        exit()
else:
    print("Likes")

猜你喜欢

转载自blog.csdn.net/zcx1203/article/details/81607446