Skip to content

Commit 93adcea

Browse files
kimoliviaOlivia KimBethanyG
authored andcommitted
Implement new concept exercise - string methods #2175
* Add new concept exercise - string methods * Fix formatting * Use prettier to clean up markdown * Resolve PR comments * Fix closing bracket * PR comment, add concept files * Update languages/exercises/concept/string-methods/.meta/design.md * Update languages/concepts/string-methods/about.md * Update languages/concepts/string-methods/about.md * Update languages/concepts/string-methods/about.md * Update languages/exercises/concept/string-methods/.docs/introduction.md Co-authored-by: Olivia Kim <[email protected]> Co-authored-by: BethanyG <[email protected]>
1 parent bc5c7ff commit 93adcea

File tree

10 files changed

+217
-0
lines changed

10 files changed

+217
-0
lines changed

concepts/string-methods/about.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## String Methods in Python
2+
3+
A string (`str`) in Python is a sequence of Unicode code points which
4+
include letters, numbers, symbols, punctuation, etc. Strings
5+
implement all of the [common sequence operations](https:/docs.python.org/3/library/stdtypes.html#typesseq-common),
6+
along with iteration using the `for item in <string>` syntax.
7+
8+
Python provides a number of useful methods that you can use to manipulate
9+
strings. These string methods can be used for cleaning, splitting, translating,
10+
or otherwise working with the `str` type. New strings can be created based
11+
on method arguments, and/or additional information can be returned. Strings
12+
can be concatenated using the `+` operator or with `str.join()`.
13+
14+
Some of the more commonly used methods include:
15+
16+
- Checking for prefixes/suffixes with `startwith()` and `endswith()`
17+
- Altering string casing with methods like `upper()`, `lower()`, and `swapcase()`
18+
- Removing leading or trailing characters from a string using `strip()`, `lstrip()`, or `rstrip()`
19+
- Replacing substrings with the `replace()` method
20+
- Checking for the existence of a substring with `in`
21+
- Concatenating strings with the `+` operator or `str.join()`
22+
23+
The `str` type is _immutable_, so all of these methods will return a new `str` instead of modifying the existing one.
24+
25+
For more information, you can check out the
26+
[official documentation](https://docs.python.org/3/library/stdtypes.html#string-methods).

concepts/string-methods/links.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"url": "https://docs.python.org/3/library/stdtypes.html#string-methods",
4+
"description": "string methods"
5+
},
6+
{
7+
"url": "https://docs.python.org/3/library/stdtypes.html#common-sequence-operations",
8+
"description": "common sequence operations"
9+
},
10+
{
11+
"url": "https://realpython.com/python-string-formatting/",
12+
"description": "string formatting best practices"
13+
},
14+
{
15+
"url": "https://www.programiz.com/python-programming/string",
16+
"description": "more string operations and functions"
17+
}
18+
]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## General
2+
3+
- [Introduction to string methods in Python][string-method-docs]
4+
5+
## 1. Capitalize the title of the paper
6+
7+
- You can use [string methods][title-method-docs] to capitalize the title properly.
8+
9+
## 2. Check if each sentence ends with a period
10+
11+
- You can use [string methods][endwith-method-docs] to check the ending of a string.
12+
13+
## 3. Clean up spacing
14+
15+
- You can use [string methods][strip-method-docs] to remove whitespace.
16+
17+
## 4. Replace words with a synonym
18+
19+
- You can use [string methods][replace-method-docs] to replace words.
20+
21+
[string-method-docs]: https://docs.python.org/3/library/stdtypes.html#string-methods
22+
[title-method-docs]: https://docs.python.org/3/library/stdtypes.html#str.title
23+
[endswith-method-docs]: https://docs.python.org/3/library/stdtypes.html#str.endswith
24+
[strip-method-docs]: https://docs.python.org/3/library/stdtypes.html#str.strip
25+
[replace-method-docs]: https://docs.python.org/3/library/stdtypes.html#str.replace
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
In this exercise you are helping your younger sister edit her paper for school. The teacher is looking for correct punctuation, grammar, and excellent word choice.
2+
3+
You have four tasks to clean up and modify strings.
4+
5+
## 1. Capitalize the title of the paper
6+
7+
Any good paper needs a properly formatted title. Implement the function `capitalize_title()` which takes in the parameter `title` and capitalizes the first letter of each word of the title. This function will return a `str`.
8+
9+
```python
10+
>>> capitalize_title("my hobbies")
11+
"My Hobbies"
12+
```
13+
14+
## 2. Check if each sentence ends with a period
15+
16+
You want to make sure that the punctuation in the paper is perfect. Implement the function `check_sentence_ending()` that takes in the parameter `sentence`. This function should return a `bool`.
17+
18+
```python
19+
>>> check_sentence_ending("I like to hike, bake, and read.")
20+
True
21+
```
22+
23+
## 3. Clean up spacing
24+
25+
To make the paper look professional, unnecessary spacing needs to be removed. Implement the function `remove_extra_spaces()` which takes in the parameter `sentence` and removes extra whitespace at the beginning and at the end of the sentence. This function should return a `str`.
26+
27+
```python
28+
>>> remove_extra_spaces(" I like to go on hikes with my dog. ")
29+
"I like to go on hikes with my dog."
30+
```
31+
32+
## 4. Replace words with a synonym
33+
34+
To make the paper even better, you can replace some of the adjectives with synonyms. Write a function `replace_word_choice()` that takes in the parameters `sentence`, `old_word`, and `new_word`. This function should replace all instances of the `old_word` with the `new_word` and return the updated sentence. The function should return a `str`.
35+
36+
```python
37+
>>> replace_word_choice("I bake good cakes", "good", "amazing")
38+
"I bake amazing cakes"
39+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## string-methods
2+
3+
Python provides a number of useful methods that you can use to manipulate
4+
strings. These methods can be used for cleaning, splitting, translating,
5+
or otherwise working with the str type. New strings can be created based
6+
on method arguments, and/or additional information can be returned. Strings
7+
can be concatenated using the `+` operator or with `str.join()`. Strings
8+
also implement all of the common sequence operations along with iteration using
9+
the `for item in <string>` syntax.
10+
11+
`Strings` are immutable (meaning the value does not change), so each of these
12+
methods will actually return a new instance of `str` instead of modifying
13+
the existing one.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"authors": [
3+
{
4+
"github_username": "kimolivia",
5+
"exercism_username": "olivia"
6+
}
7+
]
8+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## Design
2+
3+
## Goal
4+
5+
The goal of this exercise is to teach helpful string manipulation methods and how to use them in Python.
6+
7+
## Learning objectives
8+
9+
- Know the `str` class and its core methods
10+
- Know when to use methods of str to operate on a string
11+
- Use several of the methods from each grouping below to operate on strings
12+
- Check if a given string ends/begins with the a specified suffix/prefix with the `endswith()`/ `startswith()` methods
13+
- Check if a given string is composed of certain code points/characters by using `isalnum()`, `isalph()`, `isascii()`, `isdecimal()`, `isdigit()`, or `isnumeric()` methods.
14+
- Return a new string that is a transform of the input string using `capitalize()`, `lower()`, `casefold()`, `swapcase()`, `upper()`, or `title()` methods
15+
- Return a new string that has leading/trailing whitespaces removed with `lstrip()`, `rstrip()`, or `strip()` methods
16+
- Check if a given substring is contained within a given string using `in`
17+
- Return a new string that replaces a given substring with a new value using the `replace()` method
18+
- Build a new string from an input iterable with `join()`
19+
20+
## Out of scope
21+
22+
- string formatting methods, "f-strings", "format strings"
23+
- string splitting
24+
- string translate methods (will have their own exercise)
25+
- string constants from the string module
26+
- built-ins that can take a str as an argument
27+
- encoding/decoding & codecs
28+
- the binary sequence types bytes, bytearray, and memoryview
29+
- binary transforms and text transforms
30+
- text encodings
31+
32+
## Concepts
33+
34+
- `string-methods`
35+
36+
## Prerequisites
37+
38+
- `booleans`
39+
- `strings`
40+
- `sequences`, `sequence type`
41+
- `iteration`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def capitalize_title(title):
2+
return title.title()
3+
4+
def check_sentence_ending(sentence):
5+
return sentence.endswith(".")
6+
7+
def clean_up_spacing(sentence):
8+
new_sentence = sentence.strip()
9+
return new_sentence
10+
11+
def replace_word_choice(sentence, new_word, old_word):
12+
better_sentence = sentence.replace(new_word, old_word)
13+
return better_sentence
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def capitalize_title(title):
2+
pass
3+
4+
def check_sentence_ending(sentence):
5+
pass
6+
7+
def remove_extra_spaces(sentence):
8+
pass
9+
10+
def replace_word_choice(sentence, new_word, old_word):
11+
pass
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
from str_methods import *
3+
4+
5+
class test_string_methods(unittest.TestCase):
6+
def test_capitalize_title(self):
7+
self.assertEqual(capitalize_title("fish are cold blooded"),
8+
"Fish Are Cold Blooded")
9+
10+
def test_sentence_ending(self):
11+
self.assertEqual(check_sentence_ending("Snails can sleep for 3 years."), True)
12+
13+
def remove_extra_spaces(self):
14+
self.assertEqual(remove_extra_spaces(" Elephants can't jump. "),
15+
"Elephants can't jump.")
16+
17+
def test_replace_word_choice(self):
18+
self.assertEqual(replace_word_choice("Animals are cool", "cool", "awesome"),
19+
"Animals are awesome")
20+
21+
def test_replace_word_not_exist(self):
22+
self.assertEqual(replace_word_choice("Animals are cool", "small", "tiny"),
23+
"Animals are cool")

0 commit comments

Comments
 (0)