Skip to content

Commit e1f41f1

Browse files
More notes on strings
1 parent c6a6728 commit e1f41f1

File tree

1 file changed

+58
-4
lines changed

1 file changed

+58
-4
lines changed

strings.md

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ print(x)
77
print(x[3])
88
print(x[1:3])
99
print(x + ' there')
10+
print(len(x))
11+
for c in x:
12+
print(c)
1013
```
1114

12-
But you cannot modify a list, you have to copy it:
15+
But you cannot modify a string like you modify a list, you have to copy it:
1316
```python
1417
x = "hello"
1518
x[3] = 'p'
@@ -31,12 +34,63 @@ name = input("What's your name? ")
3134
print(f"Hello, {name}")
3235
```
3336

34-
More information on strings:
37+
## Problems
38+
39+
- Write a program that prints each letter of a string and its index
40+
41+
- Write a program that computes the length of a string (without using the `len` function, of course)
42+
43+
- Write a program to ask the user for their full name and converts it into an email address. For example:
44+
```
45+
Enter your full name: Boaty McBoatface
46+
Your email is boaty.mcboatface@takenolab.com
47+
```
48+
49+
- Write a program that counts the number of words in a string. For example:
50+
```
51+
Enter a string: The quick brown fox
52+
The string has 4 words in it
53+
```
54+
55+
- Write a program that checks if a string is a number. For example:
56+
```
57+
Enter a string: 1234
58+
The string is a number
59+
Enter a string: 1234e
60+
The string is not a number
61+
Enter a string: f42kl
62+
The string is not a number
63+
```
64+
65+
- Write a program that checks if a string is in snake case. For example:
66+
```
67+
Enter a string: a_python_variable
68+
The string is in snake case
69+
Enter a string: aPythonVariable
70+
The string is not in snake case
71+
Enter a string: aPython_variable
72+
The string is not in snake case
73+
```
74+
75+
- Write a program that checks if a string is in camel case. For example:
76+
```
77+
Enter a string: a_python_variable
78+
The string is not in camel case
79+
Enter a string: aPythonVariable
80+
The string is in camel case
81+
Enter a string: aPython_variable
82+
The string is not in camel case
83+
```
84+
85+
- Write a program that converts a string from snake case to camel case, and another program that converts a string from camel case to snake case.
86+
87+
Once you have completed the problems above, look at the Python documentation to see the inbuilt string functions:
3588
https://docs.python.org/3/library/stdtypes.html#str
89+
- Can you simplify your answers to the above problems using the inbuilt functions?
3690
37-
Some problems:
91+
### Some more problems:
3892
39-
https://py.checkio.org/en/mission/acceptable-password-i/
93+
https://py.checkio.org/en/mission/acceptable-password-i/ (and then do parts II, III, etc)
4094
4195
https://py.checkio.org/en/mission/backward-string/
4296

0 commit comments

Comments
 (0)