Commit bf008f16 authored by Swicech's avatar Swicech
Browse files

upload assignment 7

parent cc50d7f2
%% Cell type:markdown id: tags:
# Accessing Characters in a String
**Learning goals**
* Strings
* Functions
**Starting Out with Python:**
* Ch. 8.1
This task is about how to access and return the characters in a string.
%% Cell type:markdown id: tags:
### String Accessing Basics
%% Cell type:markdown id: tags:
**Iterating through strings:**
It is possible to iterate through string by using a for-loop. The general way of doing it is:
%% Cell type:code id: tags:
``` python
for character in string:
# do something
```
%% Cell type:markdown id: tags:
For each iteration, the `character`variable will refer to a character in `string`, and it will start by referencing the first character. Try to run the code below.
%% Cell type:code id: tags:
``` python
name = "Bob Bernt"
for ch in name:
print(ch)
```
%% Cell type:markdown id: tags:
**Indexing:**
Another way of accessing the characters in a string is to use indexing (same as you can use to access elements in a list). Every character in a string has an index that represents it's placement in the string:
![img](./../Resources/Images/streng-eksempel.png)
As you can see in the picture above, the indexing starts at 0, just as in list indexing. To access one of the characters and save it to a variable, you write
`char = my_string[3]`
This will assignment `char` to `'e'`
Every character in the string can be accessed by *two* numbers, one positive and one negative. If you want the last character in the string(with length 13) you can use 12 or -1 as the index: `char = my_string[12]` and `char = my_string[-1]` will both assign `char` to `d`.
%% Cell type:code id: tags:
``` python
# Try running the code
my_string = 'Roses are red'
for i in range(5):
print(my_string[i])
for j in range(-7,0,1):
print(my_string[j])
```
%% Cell type:markdown id: tags:
### a)
%% Cell type:markdown id: tags:
Write a function that takes a string as argument, iterates through this and prints each character to the screen.
**Example**:
```python
#Argument is `Nilfgaard`
N
i
l
f
g
a
a
r
d
```
***Write code below:***
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
### b)
%% Cell type:markdown id: tags:
Write a function named `third_letter(string)` which takes a string as argument and return the third character in the string. If the length of the string is too short, the function should return `'q'`'.
***Write code below:***
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
Check that the code below produces the correct output.
%% Cell type:code id: tags:
``` python
print(third_letter("Temeria")) # correct is 'm'
print(third_letter("IT")) # correct is 'q'
```
%% Cell type:markdown id: tags:
### c)
%% Cell type:markdown id: tags:
Write a function that takes a string as argument and return the largest valid index. I.e. the largest index which will not give IndexError.
**Example run:**
```python
#Argument is "The White Wolf"
13
#Argument is "Redania"
6
```
***Write code below:***
%% Cell type:code id: tags:
``` python
```