Strings - Learn Python 3 - Snakify

# Lesson 5.Strings

## Problem «Slices»

#### Statement

You are given a string.

In the first line, print the third character of this string.

In the second line, print the second to last character of this string.

In the third line, print the first five characters of this string.

In the fourth line, print all but the last two characters of this string.

In the fifth line, print all the characters of this string with even indices (remember indexing starts at 0, so the characters are displayed starting with the first).

In the sixth line, print all the characters of this string with odd indices (i.e. starting with the second character in the string).

In the seventh line, print all the characters of the string in reverse order.

In the eighth line, print every second character of the string in reverse order, starting from the last one.

In the ninth line, print the length of the given string.

## Problem «The number of words»

#### Statement

Given a string consisting of words separated by spaces. Determine how many words it has. To solve the problem, use the method count.

## Problem «The two halves»

#### Statement

Given a string. Cut it into two "equal" parts (If the length of the string is odd, place the center character in the first string, so that the first string contains one more characther than the second). Now print a new string on a single row with the first and second halfs interchanged (second half first and the first half second)

Don't use the statement if in this task.

## Problem «To swap the two words»

#### Statement

Given a string consisting of exactly two words separated by a space. Print a new string with the first and second word positions swapped (the second word is printed first).

This task should not use loops and if.

## Problem «The first and last occurrence»

#### Statement

Given a string that may or may not contain a letter of interest. Print the index location of the first and last appearance of f. If the letter f occurs only once, then output its index. If the letter f does not occur, then do not print anything.

Don't use loops in this task.

## Problem «The second occurrence»

#### Statement

Given a string that may or may not contain the letter of interest. Print the index location of the second occurrence of the letter f. If the string contains the letter f only once, then print -1, and if the string does not contain the letter f, then print -2.

## Problem «Remove the fragment»

#### Statement

Given a string in which the letter h occurs at least twice. Remove from that string the first and the last occurrence of the letter h, as well as all the characters between them.

## Problem «Reverse the fragment»

#### Statement

Given a string in which the letter h occurs at least two times, reverse the sequence of characters enclosed between the first and last appearances.

## Problem «Replace the substring»

#### Statement

Given a string. Replace in this string all the numbers 1 by the word one.

## Problem «Delete a character»

#### Statement

Given a string. Remove from this string all the characters @.

## Problem «Replace within the fragment»

#### Statement

Given a string. Replace every occurrence of the letter h by the letter H, except for the first and the last ones.

## Problem «Delete every third character»

#### Statement

Given a string. Delete from it all the characters whose indices are divisible by 3.