N
N
Nicola12222021-07-22 17:46:14
Python
Nicola1222, 2021-07-22 17:46:14

How to format text correctly?

Hello!
There is a string We need to make two variables a1 , a2 - And put minutes and seconds from the string into each of them stroka a1 = 1 | (minutes) a2 = 10 | (seconds) How can this be done?
stroka = 'Время до отключения 1 минута 10 секунд'



Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2021-07-22
@Nicola1222

If your string always has this format Time to Shutdown X minutes Y seconds , then one option is to split it into individual words using the split() method, and then select the necessary values. If necessary, convert them to a number.

stroka = 'Время до отключения 1 минута 10 секунд'
stroka_list = stroka.split() #получим ['Время', 'до', 'отключения', '1', 'минута', '10', 'секунд']
a1 = int(stroka_list[3]) 
a2 = int(stroka_list[5])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question