Get the first half of the string stored in the variable called phrase

Рейтинг: 0Ответов: 1Опубликовано: 01.11.2014

Доброе!

Начинаю изучать python. Не могу понять одну задачу:

len() function is used to count how many characters long a string is.
Get the first half of the string stored in the variable called phrase. Remember about string slicing

Код:

phrase = """
It is a really long string
triple-quoted strings are used
to define multi-line strings
"""
first_half = type here
print(first_half)

Куда копать?

Ответы

▲ 1

Копайте сюда:

first_half = phrase[:len(phrase)/2]

Но есть нюансы. Как считать половину, если количество символов нечетное? У вас строка начинается с символа перевода строки, учитывать его или нет и т. д.