|
| 1 | +title: Basic Data Types in Python 3: Strings |
| 2 | +slug: python-basic-data-types-strings |
| 3 | +meta: Learn to create and manipulate strings in this series of blog posts on basic data types in Python programming |
| 4 | +category: post |
| 5 | +date: 2019-10-18 |
| 6 | +modified: 2019-10-18 |
| 7 | +newsletter: False |
| 8 | +headerimage: /img/191018-python-basic-data-types-strings/header.jpeg |
| 9 | +headeralt: Learn basic Python data types in TwilioQuest 3 - Strings |
| 10 | + |
| 11 | +If you're [new to the language](https://wiki.python.org/moin/BeginnersGuide/Overview), |
| 12 | +there's a lot to learn on your Python journey. Once you're comfortable writing |
| 13 | +and executing code, your first stop becomes learning how to represent data in |
| 14 | +your code. No matter the language, there are a few basic data types you'll use |
| 15 | +all the time - strings, numbers, booleans, lists, and dictionaries. |
| 16 | + |
| 17 | +Those data types, and how to use them in Python 3, are the topic of this blog |
| 18 | +post series. Today, we're starting with __strings__. |
| 19 | + |
| 20 | +If you're learning Python, you might also want to |
| 21 | +[check out TwilioQuest 3](https://www.twilio.com/quest/download). |
| 22 | +You'll learn about basic data types and much more about Python programming. |
| 23 | + |
| 24 | +Ready to learn how to use strings in Python 3? Let's get started! |
| 25 | + |
| 26 | +## Strings in Python 3 |
| 27 | + |
| 28 | +One of the most common data types in any programming language is a `string`. A |
| 29 | +__string__ represents a series of characters, which you would use to represent |
| 30 | +usernames, blog posts, tweets, or any text content in your code. You can create |
| 31 | +a string and assign it to a variable like this. |
| 32 | + |
| 33 | +```python |
| 34 | +my_name = "Jonathan Joestar" |
| 35 | +``` |
| 36 | + |
| 37 | +### Strings are "immutable" |
| 38 | + |
| 39 | +In Python, strings are considered [immutable](https://www.merriam-webster.com/dictionary/immutable) - |
| 40 | +once you create them, they can't be changed. You can, however, use a variety of |
| 41 | +methods to create new strings from existing strings. This type of work in |
| 42 | +programming is called __string manipulation__. Some web developers joke that at |
| 43 | +the end of the day, their job is just mashing strings together - and this isn't |
| 44 | +far from the truth! |
| 45 | + |
| 46 | +Here are some common tasks you might undertake when using strings in your code. |
| 47 | + |
| 48 | +### Common task - combining strings together |
| 49 | + |
| 50 | +Combining strings together - __concatenating__ them - is a very common task. In |
| 51 | +Python 3, you can use the `+` operator for this purpose. You can use the `+` |
| 52 | +operator multiple times to concatenate multiple strings. |
| 53 | + |
| 54 | +```python |
| 55 | +first_name = "Jonathan" |
| 56 | +last_name = "Joestar" |
| 57 | + |
| 58 | +full_name = first_name + " " + last_name |
| 59 | +``` |
| 60 | + |
| 61 | +### Common task - inserting data into strings |
| 62 | + |
| 63 | +Another common task with strings is inserting data into a specific place |
| 64 | +within a string. In programming, we call this __string interpolation__. Python 3 |
| 65 | +provides a handy tool for doing this called ["f" strings](https://www.python.org/dev/peps/pep-0498/). |
| 66 | +The "f" in "f strings" stands for __format__ - you can insert other data from |
| 67 | +your program into a string when you define it rather than doing complex string |
| 68 | +concatenation as demonstrated previously. |
| 69 | + |
| 70 | +Here is an example of creating a formatted string - note the letter `f` is |
| 71 | +included just before the first double quote when defining the `message` variable. |
| 72 | +When you want to insert data from your program into the string, you can include |
| 73 | +it between two "curly braces" - the `{` and `}` characters. |
| 74 | + |
| 75 | +```python |
| 76 | +first_name = "Jonathan" |
| 77 | +last_name = "Joestar" |
| 78 | +age = 24 |
| 79 | + |
| 80 | +message = f"My name is {first_name} {last_name}, and I am {age} years old." |
| 81 | +print(message) |
| 82 | +``` |
| 83 | + |
| 84 | +### Common task - using built-in string methods to manipulate strings |
| 85 | + |
| 86 | +String objects have a number of [methods](https://docs.python.org/3/library/stdtypes.html#string-methods) |
| 87 | +to perform common tasks, like changing the case of strings or trimming their |
| 88 | +content. Below, you'll find a few examples. In two of these examples, we are |
| 89 | +creating a string variable, and then assigning the same variable a new value, |
| 90 | +which is the result of calling a method on a string object. |
| 91 | + |
| 92 | +__Example 1:__ Convert a string to all caps using the `upper` method. |
| 93 | + |
| 94 | +```python |
| 95 | +example_string = "am I stoked enough yet?" |
| 96 | +example_string = example_string.upper() |
| 97 | +print(example_string) # prints "AM I STOKED ENOUGH YET?" |
| 98 | +``` |
| 99 | + |
| 100 | +__Example 2:__ Replace all instances of the word `kale` with `tacos`. |
| 101 | + |
| 102 | +```python |
| 103 | +example_string = "We're having kale for dinner! Yay kale!" |
| 104 | +example_string = example_string.replace("kale", "tacos") |
| 105 | +print(example_string) # prints "We're having tacos for dinner! Yay tacos!" |
| 106 | +``` |
| 107 | + |
| 108 | +__Example 3:__ Split a comma-delimited string into a list of strings. |
| 109 | + |
| 110 | +```python |
| 111 | +example_string = "Apples,Oranges,Pears" |
| 112 | +groceries = example_string.split(',') |
| 113 | + |
| 114 | +# Code below prints: |
| 115 | +# Apples |
| 116 | +# Oranges |
| 117 | +# Pears |
| 118 | +for item in groceries: |
| 119 | + print(item) |
| 120 | +``` |
| 121 | + |
| 122 | +[Check our more strings can do](https://docs.python.org/3/library/stdtypes.html#string-methods) |
| 123 | +in the Python 3 docs! |
| 124 | + |
| 125 | +## Type casting |
| 126 | + |
| 127 | +Frequently, you will want to convert data from one type into another. In |
| 128 | +programming, we call this process __type casting__. There are a number of |
| 129 | +__functions__ built in to Python which allow us to do these type conversions |
| 130 | +on basic data types. |
| 131 | + |
| 132 | +__Example 1:__ Convert a number into a string using the `str` function. |
| 133 | + |
| 134 | +```python |
| 135 | +example_number = 42 |
| 136 | +converted = str(example_number) |
| 137 | +message = "The meaning of life is " + converted |
| 138 | +``` |
| 139 | + |
| 140 | +__Example 2:__ Convert a string into a whole number (integer) using `int`. |
| 141 | + |
| 142 | +```python |
| 143 | +example_string = "2" |
| 144 | +converted = int(example_string) |
| 145 | +message = f"Two plus two equals { converted + 2 }" |
| 146 | +``` |
| 147 | + |
| 148 | +## Wrapping up |
| 149 | + |
| 150 | +Strings of text are one of the most common pieces of data you will work with |
| 151 | +in programming. Hopefully, you've learned a bit about how to work with strings |
| 152 | +in Python 3! Stay tuned for more blog posts in this series to learn more about |
| 153 | +basic data types like strings, numbers, booleans, lists, and dictionaries. |
| 154 | + |
| 155 | +Also, be sure to [download and play TwilioQuest 3](https://www.twilio.com/quest/download) |
| 156 | +to learn even more about the Python programming language! |
0 commit comments