Introduction To Strings
Introduction To Strings
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
},
"colab": {
"provenance": [],
"collapsed_sections": [
"6XqIFlaYmr1H",
"EhEAYjRnmr1U",
"03n7vTXRmr2E",
"cF2zYtzdmr2G",
"Uk9EwCKRmr2L",
"ikQx6g8tmr2P",
"YPKnWGBFmr2k",
"-Q_dsma-mr2t",
"uOh2_4hKmr24",
"B6x3RSMkmr3A"
]
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "tdT_IP3Pmr0m"
},
"source": [
"## Introduction to Strings "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "i_QBxEJymr0n"
},
"source": [
"* Strings are used in Python to record text information, such as names. It
could either be a word, a phrase, a sentence, a paragraph or an entire
encyclopedia. Strings in Python are actually a *sequence*, which basically
means Python keeps track of every element in the string as a sequence. For example,
Python understands the string \"joker' to be a sequence of letters in a specific
order. This means we will be able to use indexing to grab particular letters (like
the first letter, or the last letter).\n",
"\n",
"\n",
"* This idea of a sequence is an important one in Python and we will touch
upon it later on in the future."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "w_7O10QKmr0o"
},
"source": [
"### Creating a String\n",
"* To create a string in Python you need to use either single quotes or
double quotes. For example:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "XMd1nYlKmr0p",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "14f7df32-8a5b-4642-8ef5-e115d9af2eff"
},
"source": [
"# Single word\n",
"my_first_string= 'algebra'\n",
"my_first_string"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'algebra'"
]
},
"metadata": {
"tags": []
},
"execution_count": 23
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "iaz2basTmr0t",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f275b05f-19db-40b5-cf85-f9855065868f"
},
"source": [
"# Entire phrase \n",
"phrase = 'Statistics sits at the heart of machine learning'\n",
"print(phrase)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Statistics sits at the heart of machine learning\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "HElQZWE569tQ",
"outputId": "ec81048d-d2f8-4cad-d0cd-238d557136e2"
},
"source": [
"# Statement to get the type of the variable\n",
"type(phrase)"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"str"
]
},
"metadata": {
"tags": []
},
"execution_count": 26
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "r77SuNlPmr0v",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "7daf4da9-8228-421d-da40-5a0e911a302a"
},
"source": [
"# We can also use double quote\n",
"my_string = \"String built with double quotes\"\n",
"print(my_string) # Use the print command "
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"String built with double quotes\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "RZj7MWrMmr0y",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "5ec836e3-c590-4dfd-a42e-c9bf3abd1b75"
},
"source": [
"# Be careful with quotes!\n",
"sentence= 'I'm using single quotes, but this will create an error'\n",
"print(sentence)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"I'm using single quotes, but this will create an error\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Xn10egj7mr00"
},
"source": [
"* The reason for the error above is because the single quote in
<code>I'm</code> stopped the string. You can use combinations of double and single
quotes to get the complete statement."
]
},
{
"cell_type": "code",
"metadata": {
"id": "SyVPr5ACmr00",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "29fae8c7-d14a-4d42-dbb8-1ea7af7d3cf5"
},
"source": [
"sentence= \"I'm using single quotes, but this will create an error\"\n",
"print(sentence)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"I'm using single quotes, but this will create an error\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "QNrQSfSMdJqu",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "78ae1566-9112-49d6-a3ee-2edce550ccca"
},
"source": [
"hashtag = \"#\"\n",
"print(hashtag)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"#\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "G9N2tqNI9Uv5",
"outputId": "9054fd1e-6113-4c52-df31-a41272dbed61"
},
"source": [
"type(hashtag)"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"str"
]
},
"metadata": {
"tags": []
},
"execution_count": 31
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xuXCjPSGmr04"
},
"source": [
"### How to print strings"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "is3z5n_lmr04"
},
"source": [
"* 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",
"metadata": {
"id": "yBTUPuy1mr05",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "a03297a1-fee9-49ba-9987-dbf0f0e74239"
},
"source": [
"# We can simply declare a string\n",
"'Deep Learning'"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'Deep Learning'"
]
},
"metadata": {
"tags": []
},
"execution_count": 33
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "zT7Fa4THmr08",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "50553e6a-d51b-414d-863e-2c542235af1f"
},
"source": [
"# Note that we can't output multiple strings this way\n",
"'Linear Algebra'\n",
"'Calculus'"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'Calculus'"
]
},
"metadata": {
"tags": []
},
"execution_count": 34
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "r0F7SS9Smr0_"
},
"source": [
"### We can use the <code>print()</code> statement to print a string."
]
},
{
"cell_type": "code",
"metadata": {
"id": "eBQY27Yrmr1A",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "de218e66-81fd-453a-e0b1-6823dda27b03"
},
"source": [
"# print('Linear Algebra')\n",
"# print('Calculus')\n",
"print('Use \\n to print a new line')\n",
"print('\\n')\n",
"print('See what \\n I mean?')"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Use \n",
" to print a new line\n",
"\n",
"\n",
"See what \n",
" I mean?\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vlVtl911mr1D"
},
"source": [
"## Playing with strings"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vjXDmhwLmr1D"
},
"source": [
"* We can also use a function called <code>len()</code> to check the length
of a string!"
]
},
{
"cell_type": "code",
"metadata": {
"id": "otU_-I1Bmr1E",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "1cd94aef-54d6-4e39-abd4-2c5e67b515ee"
},
"source": [
"algo = 'regres sion '\n",
"len(algo)"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"12"
]
},
"metadata": {
"tags": []
},
"execution_count": 41
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wVMqyh4Gmr1G"
},
"source": [
"### <b> Python's built-in len() function counts all of the characters in
the string, including spaces and punctuation. </b>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6XqIFlaYmr1H"
},
"source": [
"### <u> String Indexing </u>\n",
"\n",
"* We know strings are a sequence, which means Python can use indexes to
call parts of the sequence.\n",
"\n",
"\n",
"* A string index refers to the location of an element present in a
string.\n",
"\n",
"\n",
"* The indexing begins from 0 in Python. \n",
"\n",
"\n",
"* The first element is assigned an index 0, the second element is assigned
an index of 1 and so on and so forth.\n",
"\n",
"\n",
"* In Python, we use brackets <code>[]</code> after an object to call its
index."
]
},
{
"cell_type": "code",
"metadata": {
"id": "W1aqQ0DQmr1H"
},
"source": [
"# Assign string as a string\n",
"string = 'Principal Component Analysis!'"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Vx4emEz2mr1K",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "32b4f043-5115-4733-96ee-500af7cbccee"
},
"source": [
"# Print the object\n",
"print(string) "
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Principal Component Analysis!\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rHxN8nKNmr1N"
},
"source": [
"* Let's start indexing!"
]
},
{
"cell_type": "code",
"metadata": {
"id": "8hmOB4UOmr1O",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "63711639-a804-4beb-f2e2-e4164ca82544"
},
"source": [
"# Show first element (in this case a letter)\n",
"print(string[5])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"i\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "_-ut3Whcmr1Q",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "d638a154-f0d3-4f41-8245-f0173844a980"
},
"source": [
"print(string[15])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"n\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "fQsgVUr8icwU",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "be3feb3b-a905-4423-e850-a4182d0aa413"
},
"source": [
"len(string)"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"29"
]
},
"metadata": {
"tags": []
},
"execution_count": 47
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "AdYupakmmr1S",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "e574e23c-e984-4ff3-ce39-824d6c7e1d37"
},
"source": [
"# Grab the element at the index -1, which is the LAST element\n",
"print(string[28])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"!\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "S--o1sizE92w",
"outputId": "909c9ab2-fd99-4ea6-cf74-a67bff58ac1a"
},
"source": [
"print(string[-2])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"s\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EhEAYjRnmr1U"
},
"source": [
"### String Slicing"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3h7kltimmr1V"
},
"source": [
"* We can use a <code>:</code> to perform *slicing* which grabs everything
up to a designated point. \n",
"\n",
"\n",
"* The starting index is specified on the left of the <code>:</code> and
the ending index is specified on the right of the <code>:</code>. \n",
"\n",
"\n",
"* Remember the element located at the right index is not included."
]
},
{
"cell_type": "code",
"metadata": {
"id": "mUroqei-mr1V",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "726d6a78-83d4-4588-be10-d62bb555abcf"
},
"source": [
"# Grab everything past the first term all the way to the length of s which
is len(s)\n",
"print(string)\n",
"print(string[:13])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Principal Component Analysis!\n",
"Principal Com\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"id": "MhnWuVT9GAx2",
"outputId": "429fb989-3eb6-4c8d-e0b1-49ac2055ec5e"
},
"source": [
"string[13]"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'p'"
]
},
"metadata": {
"tags": []
},
"execution_count": 54
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "dqDQwX-yj7pg",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "3a6539e4-582f-4b8f-b534-47a1cc983aa0"
},
"source": [
"string[12]"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'m'"
]
},
"metadata": {
"tags": []
},
"execution_count": 37
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "d6VcutfNmr1Z",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "0cdb480f-5bdf-4df0-d88d-b0b2e93ee93a"
},
"source": [
"# Grab everything starting from index 10 till index 18\n",
"print(string[10:])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Component Analysis!\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wI8WSykWmr1b"
},
"source": [
"* If you do not specify the ending index, then all elements are extracted
which comes after the starting index including the element at that starting index.
The operation knows only to stop when it has run through the entire string."
]
},
{
"cell_type": "code",
"metadata": {
"id": "rEhtYIfVmr1c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "e3acc835-8604-4368-b808-9a18be302436"
},
"source": [
"print(string[3:5])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"nc\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BEPRya3tmr1e"
},
"source": [
"* If you do not specify the starting index, then all elements are
extracted which comes befores the ending index excluding the element at the
specified ending index. The operation knows only to stop when it has extracted all
elements before the element at the ending index."
]
},
{
"cell_type": "code",
"metadata": {
"id": "f92GKgPymr1e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "95ea5960-7b19-4429-81fa-29c7fc8958d5"
},
"source": [
"print(string[2:4])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"in\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "C8le5z8Bmr1h"
},
"source": [
"* If you do not specify the starting and the ending index, it will extract
all elements of the string."
]
},
{
"cell_type": "code",
"metadata": {
"id": "3BF6pugMmr1h",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "61f88414-e5f6-4f7a-e4e1-bc4a89068c77"
},
"source": [
"#Everything\n",
"print(string[:])\n",
"print(string)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Principal Component Analysis!\n",
"Principal Component Analysis!\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "l79g0xEgmr1j"
},
"source": [
"* We can also use negative indexing to go backwards."
]
},
{
"cell_type": "code",
"metadata": {
"id": "Rj40dnalmr1j",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "84175bf9-cf7f-4c5a-dd9c-03f70dc36012"
},
"source": [
"# Last letter (one index behind 0 so it loops back around)\n",
"string[-29]"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'P'"
]
},
"metadata": {
"tags": []
},
"execution_count": 62
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AcQKQlfgmr1m"
},
"source": [
"* We can also extract the last four elements. Remember we can use the
index -4 to extract the FOURTH LAST element"
]
},
{
"cell_type": "code",
"metadata": {
"id": "jATkF52omr1m",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "4ce3a0c4-4f03-4aa9-c582-c13b2d856e40"
},
"source": [
"string[-1:-4]"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"''"
]
},
"metadata": {
"tags": []
},
"execution_count": 65
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"id": "vl0XNuYOI9uB",
"outputId": "c9c35a8d-0e98-4278-dd43-221fa9d883f4"
},
"source": [
"string[-4:]"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'sis!'"
]
},
"metadata": {
"tags": []
},
"execution_count": 67
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "L76zPkC8mr1q"
},
"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",
"metadata": {
"id": "7yl_nj5fmr1r",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "bfbec37d-3f96-44bf-afb2-da66583d9a84"
},
"source": [
"# Grab everything, but go in steps size of 1\n",
"print(string[::1])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Principal Component Analysis!\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "QHg78LgvJzNE",
"outputId": "b0841458-1731-4993-e54f-a037be06925f"
},
"source": [
"print(string[::3])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Pnp mntnys\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "ereWDp3Cmr1t",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "5787eb48-7dc1-4ed5-b89b-5ee048a2f87a"
},
"source": [
"# Grab everything, but go in step sizes of 5\n",
"print(string[5:15:5])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"iC\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"id": "rJnDRdwnKvos",
"outputId": "7bc94be8-9e73-4630-df76-c16aeeb27577"
},
"source": [
"string[::1]"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'Principal Component Analysis!'"
]
},
"metadata": {
"tags": []
},
"execution_count": 72
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "GIljHfQimr1w",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "8cdef351-350e-48ee-d1ea-0a7e6b234bb8"
},
"source": [
"# We can use this to print a string backwards\n",
"string[::-1]"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'!sisylanA tnenopmoC lapicnirP'"
]
},
"metadata": {
"tags": []
},
"execution_count": 73
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "kcaf3COcmr1y",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "d516de4d-0556-4b81-a00d-b3dfdff0f7b7"
},
"source": [
"# We can use this to print a string backwards with steps\n",
"string[2:4:-1]"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"''"
]
},
"metadata": {
"tags": []
},
"execution_count": 74
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"id": "wkioIS4ELIRo",
"outputId": "5e0ce101-28aa-482e-eb6d-ff9de3b56f05"
},
"source": [
"string[4:2:-1]"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'cn'"
]
},
"metadata": {
"tags": []
},
"execution_count": 75
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "xHczE7gypxMk",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "0da43de4-7b45-4181-9b95-b1c283369c2c"
},
"source": [
"s = 'foobar'\n",
"s[0::-3]"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'f'"
]
},
"metadata": {
"tags": []
},
"execution_count": 21
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qTlCu2phmr11"
},
"source": [
"## String Properties\n",
"\n",
"* It's important to note that strings have an important property known as
*immutability*. \n",
"\n",
"\n",
"* This means that once a string is created, the elements within it can not
be changed or replaced via item assignment. We will see how we can do such
operation using string methods"
]
},
{
"cell_type": "code",
"metadata": {
"id": "eE2jbYDMmr11",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 201
},
"outputId": "bfa89dae-185e-48f6-e8e4-3379f5df895d"
},
"source": [
"# Can we change our string 'Hello' to 'Cello'? Lets try replacing the
first letter H with C\n",
"string='Hello'\n",
"string[0] = 'C'"
],
"execution_count": null,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "ignored",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-76-f55372fc219f>\u001b[0m in \
u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \
u001b[0;31m# Can we change our string 'Hello' to 'Cello'? Lets try replacing the
first letter H with C\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\
u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mstring\
u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'Hello'\u001b[0m\u001b[0;34m\u001b[0m\
u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mstring\
u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \
u001b[0;34m=\u001b[0m \u001b[0;34m'C'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\
u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item
assignment"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cGn9h31xmr13"
},
"source": [
"* Notice how the error tells us directly what we can't do, that is we
can't change the item assignment!\n",
"\n",
"\n",
"* Something we *can* do is concatenate strings!"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Ana6cTXTmr14",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "6f9f11e5-b888-4e65-bb2d-2378119eb32a"
},
"source": [
"# Concatenate strings!\n",
"\n",
"string1='abc'\n",
"string2='def'\n",
"print(string1 + ' ' + string2 )"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"abc def\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 166
},
"id": "ouFQybUrNCgm",
"outputId": "418bfb3c-c8c6-4615-84bd-927d579a4ba0"
},
"source": [
"print(string1 + 4 + string2)"
],
"execution_count": null,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "ignored",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-79-34b0f8d1c2ec>\u001b[0m in \
u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \
u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstring1\u001b[0m \
u001b[0;34m+\u001b[0m \u001b[0;36m4\u001b[0m \u001b[0;34m+\u001b[0m \
u001b[0mstring2\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\
u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\")
to str"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "S3h79CMjmr17"
},
"source": [
"* To convert an integer into a string, you can use the <code>str()</code>
function or you can simply write the number in quotes"
]
},
{
"cell_type": "code",
"metadata": {
"id": "-bQKbbIKmr17",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "873a7cde-a05d-4590-dfd6-3d5700981ae8"
},
"source": [
"# Concatenate strings!\n",
"\n",
"string1='abc'\n",
"string2='def'\n",
"num = 4\n",
"print(string1 + str(4) + string2)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"abc4def\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "PRTTNkyWqujV",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "7fff1a14-2e6e-46b6-9196-e323d8ee07c8"
},
"source": [
"str(num)"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'4'"
]
},
"metadata": {
"tags": []
},
"execution_count": 75
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "mm6CJD-fmr19",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "9f80ff5a-3305-4387-a5c1-f844268804f8"
},
"source": [
"# Concatenate strings!\n",
"string1='abc'\n",
"string2='def'\n",
"string1 + '4'+ string2"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'abc4def'"
]
},
"metadata": {
"tags": []
},
"execution_count": 76
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "oU-CD4VbrBJ5",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "ae24d5ab-c558-4a91-fde7-d62e840f2668"
},
"source": [
"print(string)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Hello\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "N1iCkcKomr1_",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "bc69472d-73d6-4890-c85c-fc369ab48c70"
},
"source": [
"# We can reassign string completely though!\n",
"string = string + ' concatenate me!'\n",
"print(string)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Hello concatenate me! concatenate me!\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "pj2d89iKmr2A",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "a40491aa-eafa-42dd-93bd-290a4fe9d58a"
},
"source": [
"letters = 'wubba'\n",
"letters*3"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'wubbawubbawubba'"
]
},
"metadata": {
"tags": []
},
"execution_count": 85
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "duTA9_hlmr2C"
},
"source": [
"## String functions and methods"
]
},
{
"cell_type": "code",
"metadata": {
"id": "0Wy8i1-Mmr2D",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "e8a4249e-4a08-4af9-916d-cc63ac99fdd2"
},
"source": [
"algorithm = 'Neural Networks'\n",
"\n",
"print(algorithm)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Neural Networks\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "03n7vTXRmr2E"
},
"source": [
"### <code>len()</code> \n",
"\n",
"* <code>len()</code> function returns the length of the string"
]
},
{
"cell_type": "code",
"metadata": {
"id": "At6CdDUgmr2F",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "44b85688-cce9-4751-fa62-4456480a008d"
},
"source": [
"# Print the length of the string\n",
"len(algorithm)"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"15"
]
},
"metadata": {
"tags": []
},
"execution_count": 87
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cF2zYtzdmr2G"
},
"source": [
"### <code>lower()</code>\n",
"\n",
"* <code>lower()</code> method converts the string to lowercase "
]
},
{
"cell_type": "code",
"metadata": {
"id": "nX86MJxZmr2H",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "fc48bd40-085e-4bde-8a3b-d2307384d86c"
},
"source": [
"# Conver the string to lowercase\n",
"algorithm.lower()"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'neural networks'"
]
},
"metadata": {
"tags": []
},
"execution_count": 88
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "uNysVw_SsMNn",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "bf175166-692d-4964-8b8c-a48b734add4e"
},
"source": [
"print(algorithm)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Neural Networks\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "858z5CKZmr2J"
},
"source": [
"* <u> What about <code>lower(algorithm)</code>? Is it not similar to the
<code>len()</code>function?</u>\n",
"\n",
"\n",
"* <u> An important point to note here is <code>len()</code> is a function
and <code>lower()</code> is a method for a string object. It will be pretty clear
about the exact differences between a function and a method as we move ahead. </u>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "dUG6OM68mr2J",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 184
},
"outputId": "1bc77bce-6a97-4900-8a03-c9aa2b09586f"
},
"source": [
"# Lets try that out\n",
"lower(algorithm)"
],
"execution_count": null,
"outputs": [
{
"output_type": "error",
"ename": "NameError",
"evalue": "ignored",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mNameError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-90-4d609230893c>\u001b[0m in \
u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \
u001b[0;31m# Lets try that out\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\
u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mlower\
u001b[0m\u001b[0;34m(\u001b[0m\u001b[0malgorithm\u001b[0m\u001b[0;34m)\u001b[0m\
u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'lower' is not defined"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Uk9EwCKRmr2L"
},
"source": [
"### <code>upper()</code> \n",
"\n",
"* <code>upper()</code> method converts the string to uppercase "
]
},
{
"cell_type": "code",
"metadata": {
"id": "cqkxoLYwmr2M",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "dd0a6aae-9b7c-4415-a39e-e170af2a4109"
},
"source": [
"# Convert the string to uppercase\n",
"algorithm.upper()"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'NEURAL NETWORKS'"
]
},
"metadata": {
"tags": []
},
"execution_count": 91
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "yO16_FvNP3u3",
"outputId": "d42b952f-09b0-4d50-a378-15f6842c4e77"
},
"source": [
"print(algorithm)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Neural Networks\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ikQx6g8tmr2P"
},
"source": [
"### <code>count()</code> \n",
"\n",
"* <code>count()</code> method returns the count of a string in the given
string. Unlike <code>lower()</code> and <code>upper()</code> method, the
<code>count()</code> method takes a string as an argument"
]
},
{
"cell_type": "code",
"metadata": {
"id": "NZ4Z8talmr2Q",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "aad5c7f7-f49a-458f-b61f-e1777d5dc93f"
},
"source": [
"print(algorithm)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Neural Networks\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "fYzKjXf5mr2T",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "4b25aeb1-c0e7-4592-b368-996d272264c7"
},
"source": [
"algorithm.count('Ne')"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"2"
]
},
"metadata": {
"tags": []
},
"execution_count": 97
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "iM7o2uh-mr2W",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "c54542d9-8967-4fca-c95b-287b57e5c189"
},
"source": [
"algorithm.count('eu')"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"1"
]
},
"metadata": {
"tags": []
},
"execution_count": 96
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "3En7zUGmmr2c",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "6b26fdd9-82f0-472a-a3cd-3c1fc934222f"
},
"source": [
"algorithm.count(' ')"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"1"
]
},
"metadata": {
"tags": []
},
"execution_count": 98
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "omv1VDDqmr2f",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "5d21adde-f12f-4703-df2e-78b4358309b5"
},
"source": [
"algorithm.count('Neural')"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"1"
]
},
"metadata": {
"tags": []
},
"execution_count": 98
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "nvHrSXJRmr2h",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "48ed1678-1e80-4817-92b4-ab9659336b18"
},
"source": [
"algorithm.count('Neurla')"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0"
]
},
"metadata": {
"tags": []
},
"execution_count": 99
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "YPKnWGBFmr2k"
},
"source": [
"### <code>find()</code> \n",
"\n",
"* <code>find()</code> method returns the index of the first occurrence of
a string present in a given string. Similar to the <code>count()</code> method, the
<code>find()</code> method takes a string as an argument"
]
},
{
"cell_type": "code",
"metadata": {
"id": "GfkJ9pWbt5AU",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "5117d5f1-a711-4794-caab-3aaabae4f7a3"
},
"source": [
"print(algorithm)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Neural Networks\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "ILyYqHUnmr2k",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "954e2ab0-2413-482c-a82f-cf7ae94cd27b"
},
"source": [
"algorithm.find('e')"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"1"
]
},
"metadata": {
"tags": []
},
"execution_count": 101
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "8cGFl7nEmr2n",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "323abc8f-366f-4fc7-f3e9-4cfee4c076c9"
},
"source": [
"algorithm.find('Neural')"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0"
]
},
"metadata": {
"tags": []
},
"execution_count": 102
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "eCSKV08mmr2q",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "b7ea4ce9-98d3-4221-8dcd-6da9c52d47d8"
},
"source": [
"algorithm.find('Box')"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"-1"
]
},
"metadata": {
"tags": []
},
"execution_count": 103
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QnIAoEnEmr2t"
},
"source": [
"* <u>An important point to note here is that if the string which you are
looking for, is not contained in the original string, then the find method will
return a value of -1 </u>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-Q_dsma-mr2t"
},
"source": [
"### <code>replace()</code> \n",
"\n",
"* <code>replace()</code> method takes two arguments - (i) the string to
replace and (ii) the string to replace with, and returns a modified string after
the operation"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "9X7rqsDnRyiX",
"outputId": "ae5ce54e-915b-4d39-a716-dc289baa0572"
},
"source": [
"print(algorithm)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Neural Networks\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "rnokQ_6kmr2u",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "f953896e-d73e-4150-c4e7-caf69caaea3c"
},
"source": [
"algorithm.replace(' ','-')"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'Neural-Networks'"
]
},
"metadata": {
"tags": []
},
"execution_count": 105
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "9x114Rapmr2w",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "44dddaea-9e74-449c-ff88-ebef432aff31"
},
"source": [
"algorithm.replace('N','L')"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'Leural Letworks'"
]
},
"metadata": {
"tags": []
},
"execution_count": 106
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "djxLa2-Hmr2z",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "3a374470-a11f-4f97-f148-e4e2b7a2b52b"
},
"source": [
"print(algorithm)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Neural Networks\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "s2Mh-mbimr22"
},
"source": [
"* <u>Another important point worth noting here is applying any method on a
string does not actually change the original string. For example, when you print
out the algorithm string, it still contains the original value 'Neural
Networks'</u>\n",
"\n",
"\n",
"* <u> We need to store the modified string in another variable"
]
},
{
"cell_type": "code",
"metadata": {
"id": "tW013YBJmr22",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "cd8cc1b4-ee4e-4ba8-f858-d0dcb23a1f9c"
},
"source": [
"# Storing the modified string\n",
"algorithm_revised = algorithm.replace('Neural','Artificial Neural')\n",
"print(algorithm_revised)\n",
"print(algorithm)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Artificial Neural Networks\n",
"Neural Networks\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uOh2_4hKmr24"
},
"source": [
"### Printing strings a bit differently"
]
},
{
"cell_type": "code",
"metadata": {
"id": "AJjdr5ghmr24",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "8725a50c-b976-477e-bf3f-2c67468f8fd7"
},
"source": [
"first_name = 'Rahul'\n",
"last_name = 'Modi'\n",
"\n",
"full_name = f'Left plus right makes {first_name} {last_name}' # Use {}
to print the variable you want to\n",
"print(full_name)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Left plus right makes Rahul Modi\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "K-p0nqpjwANZ",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "32dd6355-b248-41b0-c333-afb246458475"
},
"source": [
"print(first_name + ' '+last_name)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Rahul Modi\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "qIHd9TJ_RTZs"
},
"source": [],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "2CR9yok8mr29",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "170ed227-7dce-44e8-9d13-83e99fcfffbc"
},
"source": [
"first_name = 'Vikash'\n",
"middle_name = ''\n",
"last_name = 'Srivastava'\n",
"\n",
"full_name = f'I am none other than {first_name} {middle_name}{last_name}.
I am a Data Scientist'\n",
"print(full_name)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"I am none other than Vikash Srivastava. I am a Data Scientist\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lXeN8B21TyZW",
"outputId": "69692b3e-3d8d-463b-af38-4cb9b8fb002b"
},
"source": [
"print(f'I am none other than {first_name} {middle_name}{last_name}. I am a
Data Scientist')"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"I am none other than Vikash Srivastava. I am a Data Scientist\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "B6x3RSMkmr3A"
},
"source": [
"### Check if a string contains a particular word or character"
]
},
{
"cell_type": "code",
"metadata": {
"id": "gxoRht92mr3A"
},
"source": [
"my_string = 'Albert Einstein'"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "NrXXZLYsxNlT",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "2269b32c-829e-4f02-cc0e-58db1eb478cb"
},
"source": [
"'Albert' in my_string"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 113
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "mlMC_fR5UPmK",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "11d444d0-a7d5-4a6d-e25e-8e42d012de84"
},
"source": [
"'Alberta' in my_string"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 114
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "yaicv9mBUS_R"
},
"source": [],
"execution_count": null,
"outputs": []
}
]
}