Skip to content

Commit

Permalink
Finish string course 2
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielbdornas committed Jun 29, 2024
1 parent a08f1b7 commit 4070071
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/blog/posts/20240627_python_course_2.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,57 @@ Get ready to join the thriving Python community, let's get started!
|---------|-----|-----|----|----|----|----|----|----|----|----|----|
| + index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| - index | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |

## Formatted Strings

- We don't need string concatenation all the time:

```python
first_name = 'Gabriel'
last_name = 'Dornas'
full_name = f'{first_name} {last_name}' # (1)!
message = f'Hello {full_name}, is everything ok with you?' # (2)!
print(message)
```

1. :man_raising_hand: The same as using string concatenation or the expression `first_name + last_name`.
2. :man_raising_hand: Mix text and variables to create new `str`.

## String methods

- Tons of code already available to use[^1]:

```python
course = 'All we need is Python!'

# lenght
print(len(course)) # (1)!

# uppercase
print(course.upper()) # (2)!

# lowercase
print(course.lower()) # (3)!

# find
print(course.find('P')) # (4)!

# replace
print(course.replace('Python', 'love')) # (5)!

# in operator
print('Python' in course) # (6)!
```

1. :man_raising_hand: Could be used, for example, to restrict the number of characters in a form's input filed.
2. :man_raising_hand: The `.upper()` method doesn't change the original `str`. Try to `print(course)` to check it.
3. :man_raising_hand: As in the `.upper()` method, the `.lower()` doesn't change the original `str`. Try to `print(course)` to check it.
4. :man_raising_hand: The `.find()` returns the index of the first occurrence of the searched character, in this example `P`. It is case sensitive, try to pass `p` to `.find()` to see the result.
5. :man_raising_hand: It's case sensitive just like we saw in the `.find()` method. Try to pass `python` to `.replace()` to see the result.
6. :man_raising_hand: This is called a boolean expression. This is case sensitive just like we saw in the `.find()` and in the `replace()` methods. Try to pass `python` to it to see the result.

??? question "Could you tell the difference between the `find()` method and the `in` operator?"

While both `find()` and `in` help search in Python, they serve different purposes. Use `in` for a quick existence check,**returning a boolean value (True/False)**. If you specifically need the position of a substring within a string, then `find()` is the way to go, **returning the index of the first occurrence**.

[^1]: Check [other `str` built-in methods](https://www.w3schools.com/python/python_ref_string.asp) that you can use in Python.

0 comments on commit 4070071

Please sign in to comment.