{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"___\n",
"\n",
"\n",
"___\n",
"
I'm
stopped the string. You can use combinations of double and single quotes to get the complete statement."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Now I'm ready to use the single quotes inside a string!\""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"Now I'm ready to use the single quotes inside a string!\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's learn about printing strings!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Printing a String\n",
"\n",
"Using Jupyter notebook with just a string in a cell will automatically output strings, but the correct way to display strings in your output is by using a print function."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello World'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# We can simply declare a string\n",
"'Hello World'"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello World 2'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Note that we can't output multiple strings this way\n",
"'Hello World 1'\n",
"'Hello World 2'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use a print statement to print a string."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello World 1\n",
"Hello World 2\n",
"Use \n",
" to print a new line\n",
"\n",
"\n",
"See what I mean?\n"
]
}
],
"source": [
"print('Hello World 1')\n",
"print('Hello World 2')\n",
"print('Use \\n to print a new line')\n",
"print('\\n')\n",
"print('See what I mean?')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## String Basics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also use a function called len() to check the length of a string!"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len('Hello World')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python's built-in len() function counts all of the characters in the string, including spaces and punctuation."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## String Indexing\n",
"We know strings are a sequence, which means Python can use indexes to call parts of the sequence. Let's learn how this works.\n",
"\n",
"In Python, we use brackets []
after an object to call its index. We should also note that indexing starts at 0 for Python. Let's create a new object called s
and then walk through a few examples of indexing."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Assign s as a string\n",
"s = 'Hello World'"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello World'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Check\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello World\n"
]
}
],
"source": [
"# Print the object\n",
"print(s) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's start indexing!"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'H'"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Show first element (in this case a letter)\n",
"s[0]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'e'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[1]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'l'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use a :
to perform *slicing* which grabs everything up to a designated point. For example:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ello World'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Grab everything past the first term all the way to the length of s which is len(s)\n",
"s[1:]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello World'"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Note that there is no change to the original s\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hel'"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Grab everything UP TO the 3rd index\n",
"s[:3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note the above slicing. Here we're telling Python to grab everything from 0 up to 3. It doesn't include the 3rd index. You'll notice this a lot in Python, where statements and are usually in the context of \"up to, but not including\"."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello World'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Everything\n",
"s[:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also use negative indexing to go backwards."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'d'"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Last letter (one index behind 0 so it loops back around)\n",
"s[-1]"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello Worl'"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Grab everything but the last letter\n",
"s[:-1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also use index and slice notation to grab elements of a sequence by a specified step size (the default is 1). For instance we can use two colons in a row and then a number specifying the frequency to grab elements. For example:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello World'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Grab everything, but go in steps size of 1\n",
"s[::1]"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'HloWrd'"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Grab everything, but go in step sizes of 2\n",
"s[::2]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'dlroW olleH'"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# We can use this to print a string backwards\n",
"s[::-1]"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## String Properties\n",
"It's important to note that strings have an important property known as *immutability*. This means that once a string is created, the elements within it can not be changed or replaced. For example:"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello World'"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'str' object does not support item assignment",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m