Sequences 0/2

Lesson 13 of 21

Strings

Read words and lines, change case, compare, count letters and check palindromes.

Part 1 of 8 Characters and indexes

Looking inside a string

A program can use a string as a whole value: read a line of text, join two strings with +, or turn the text "42" into the number 42. Many problems need to look inside the text instead. Programmers, for example, shorten a long word to its first letter, the number of letters in between and its last letter, so internationalization is written i18n and localization is written l10n. To shorten a word this way, a program has to find the word's first letter, its last letter and its length.

A program can do that because a string is a sequence of characters: letters, digits, spaces and punctuation, kept in order. The word hello is a string of five characters, h, e, l, l and o. Most of what works on a list carries over: each character has an index counted from 0, the string has a length, and you read one character by writing its index in square brackets.

Reading one character and the length

Sign up to run
s = 'hello'
print(s[0])
print(s[4])
print(len(s))
print(s[-1])
Python details

Python has no separate character type. s[0] is itself a string, one character long.

A negative index counts from the end, as it does on a list, so s[-1] is the last character.

The string hello: five characters at indexes 0 to 4
Index01234
Characterhello

s[0] is h and s[4] is o. The length is 5, so the indexes run from 0 to 4, just as they did for a list of five numbers.

There is no character at index 5, and reading s[5] stops the program with IndexError: string index out of range.

Abbreviating long words

You now have what the abbreviation from the start of this part needs. The Codeforces problem Way Too Long Words asks for it, for every word longer than 10 letters.

Some words are too long to write out. A word with more than 10 letters is abbreviated as its first letter, the number of letters between the first and the last, and its last letter. Words of 10 letters or fewer stay as they are.

The first line holds nn, and each of the next nn lines holds one word. Print each word, abbreviated when it is too long.

Constraints

  • 1≤n≤1001 \le n \le 100
  • Each word has 1 to 100 lowercase letters

Sample · Input

4
word
localization
internationalization
pneumonoultramicroscopicsilicovolcanoconiosis

Output

word
l10n
i18n
p43s

In the sample, word has 4 letters, so it stays. localization has 12 letters: its first is l, its last is n, and between them sit 12−2=1012 - 2 = 10 letters, so it becomes l10n. The 2 accounts for the first and last letters, which are written out instead of counted.

For each word the program needs three things: its length, its first character at index 0, and its last character at index length minus one.

Sign up to run
n = int(input())
for i in range(n):
    w = input()
    if len(w) > 10:
        print(w[0] + str(len(w) - 2) + w[-1])
    else:
        print(w)
Python details

input() returns the next line of input as a string, with its spaces but without the line break at its end. str(len(w) - 2) turns the number 10 into the string '10', so that + can join it to the two letters.

For pneumonoultramicroscopicsilicovolcanoconiosis, the length is 45, so the program prints p, then 45−2=4345 - 2 = 43, then s.