forked from learnpythonru/basic_exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_challenges.py
More file actions
54 lines (27 loc) · 1.35 KB
/
string_challenges.py
File metadata and controls
54 lines (27 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Вывести последнюю букву в слове
word = 'Архангельск'
print(word[10:11])
# Вывести количество букв "а" в слове
word = 'Архангельск'
print(word.count("а"))
# Вывести количество гласных букв в слове
word = 'Архангельск'
vowels = 'аеёиоуыэюяАЕЁИОУЫЭЮЯ'
vowel_count = sum(1 for char in word if char in vowels)
print(f"Количество гласных в слове '{word}': {vowel_count}")
# Вывести количество слов в предложении
sentence = 'Мы приехали в гости'
# ???
word_count = len(sentence.split())
print(f"Количество слов в предложении: {word_count}")
# Вывести первую букву каждого слова на отдельной строке
sentence = 'Мы приехали в гости'
word = sentence.split()#[Мы приехали в гости]
for i in word:
print(i[0])
# Вывести усреднённую длину слова в предложении
sentence = 'Мы приехали в гости'
words = sentence.split()
total_length = sum(len(word) for word in words)
average_length = total_length / len(words) if words else 0
print(f"Средняя длина слова: {average_length:.2f}")