网安python练习题
时间: 2023-11-21 17:59:20 浏览: 99
以下是一些网安Python练习题的例子:
1. 编写一个程序,该程序接受一个字符串并计算其中大写字母和小写字母的数量。
```python
def count_upper_lower(s):
upper_count = 0
lower_count = 0
for c in s:
if c.isupper():
upper_count += 1
elif c.islower():
lower_count += 1
return upper_count, lower_count
s = "Hello World"
upper_count, lower_count = count_upper_lower(s)
print("Number of uppercase letters:", upper_count) # 输出:2
print("Number of lowercase letters:", lower_count) # 输出:8
```
2. 编写一个程序,该程序接受一个字符串并将其中的元音字母替换为“*”。
```python
def replace_vowels(s):
vowels = "aeiouAEIOU"
for c in vowels:
s = s.replace(c, "*")
return s
s = "Hello World"
s = replace_vowels(s)
print(s) # 输出:H*ll* W*rld
```
3. 编写一个程序,该程序接受一个字符串并将其中的每个单词的首字母大写。
```python
def capitalize_words(s):
words = s.split()
capitalized_words = [word.capitalize() for word in words]
return " ".join(capitalized_words)
s = "hello world"
s = capitalize_words(s)
print(s) # 输出:Hello World
```
阅读全文