{ "cells": [ { "cell_type": "markdown", "id": "12c58d9a", "metadata": {}, "source": [ "# Strings\n", "\n", "Strings, in Python, are a fundamental data type used to represent text. Whether it's a single character or a large paragraph, any text enclosed within either single quotes ('') or double quotes (\"\") is considered a string in Python. These strings serve as the building blocks for text manipulation, and Python equips us with a rich set of built-in methods, or manipulators, to operate on strings. You can perform operations like changing letter cases, [slicing](./slicing.ipynb), and much more to manipulate and extract valuable information from text data.\n", "\n", "In Python, declaring variables is straightforward. You simply assign a value to a variable name using the assignment operator (=). For example, you can declare variables like this:" ] }, { "cell_type": "code", "execution_count": 13, "id": "45d1761f", "metadata": {}, "outputs": [], "source": [ "# Declaring basic strings\n", "name = \"Brad\"\n", "location = 'Halifax'\n", "occupation = 'DevOps Engineer'" ] }, { "cell_type": "markdown", "id": "093c372e", "metadata": {}, "source": [ "```{note}\n", "Strings, as mentioned, can be enclosed in either single quotes (`''`) or double quotes (`\"\"`), allowing you flexibility in your code. Moreover, if a string needs to contain both types of quotes, you can use the backslash (\\\\) as an escape character to ensure Python interprets the characters correctly.\n", "```" ] }, { "cell_type": "markdown", "id": "003526f9", "metadata": {}, "source": [ "## String Methods\n", "\n", "Strings can accept methods which alter the contents of the string. Some examples of string methods that are built into Python include:" ] }, { "cell_type": "code", "execution_count": 2, "id": "fd5a5d63", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "MASTERY BY ROBERT GREENE\n", "mastery by robert greene\n", "Mastery By Robert Greene\n", "Mastery by robert greene\n" ] } ], "source": [ "book_title = \"mastery by Robert greene\"\n", "print(book_title.upper())\n", "print(book_title.lower())\n", "print(book_title.title())\n", "print(book_title.capitalize())" ] }, { "cell_type": "markdown", "id": "a154226a", "metadata": {}, "source": [ "## Concatenating Strings\n", "\n", "Strings can be combined together with simple plus (`+`) signs:" ] }, { "cell_type": "code", "execution_count": 6, "id": "9ebd5109", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There was a famous scientist named Albert Einstien.\n" ] } ], "source": [ "first_name = \"Albert\"\n", "last_name = \"Einstien\"\n", "print(\"There was a famous scientist named \" + first_name + \" \" + last_name + \".\")" ] }, { "cell_type": "markdown", "id": "d0306465-d1aa-43bf-b445-36f8e532faee", "metadata": {}, "source": [ "## F-Strings\n", "\n", "Introduced in version 3.6 (3.11.5 is current as of Fall 2023), \"F-Strings\" are a much improved way to build strings in Python. Simply preceed a string with `f` before the opening quotation. Then any variable can be surrouned by curly braces `{}`, removing the need to cast variables of various types. For example:" ] }, { "cell_type": "code", "execution_count": 7, "id": "1e2023fb-579a-4ad2-861f-cfb9c6a4143e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is Jim and I am 32 years old!\n" ] } ], "source": [ "name = \"Jim\"\n", "age = 32\n", "print(f\"My name is {name} and I am {age} years old!\")" ] }, { "cell_type": "markdown", "id": "7107435d", "metadata": {}, "source": [ "## Adding & Removing Whitespace\n", "\n", "Special characters can be added to strings that add whitespace. These include:\n", "\n", "1. `\\t` which adds a tab (4 spaces)\n", "2. `\\n` which adds a newline\n", "\n", "Example:" ] }, { "cell_type": "code", "execution_count": 16, "id": "512031fc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, I'm \t \t Brad!\n", "and you are learning \n", " \t Python\n" ] } ], "source": [ "print(\"Hello, I'm \\t \\t Brad!\")\n", "print(\"and you are learning \\n \\t Python\")" ] }, { "cell_type": "markdown", "id": "0c6637c9", "metadata": {}, "source": [ "Whitespace can also be stripped out using methods. These include `strip()` (both sides), `rstrip()` (right side) and `lstrip` (left side).\n", "\n", "Example:" ] }, { "cell_type": "code", "execution_count": 17, "id": "f7732790", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello I am Albert Einstein .\n", "Hello I am Albert Einstein.\n", "Hello I am Albert Einstein.\n", "Hello I am Albert Einstein .\n" ] } ], "source": [ "name = \" Albert Einstein \"\n", "print(\"Hello I am \" + name + \".\") # whitespace remains\n", "print(\"Hello I am \" + name.strip() + \".\") # whitespace stripped\n", "print(\"Hello I am \" + name.rstrip() + \".\") # whitespace on righ stripped\n", "print(\"Hello I am \" + name.lstrip() + \".\") # whitespace on left stripped" ] } ], "metadata": { "jupytext": { "formats": "ipynb,md:myst" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.4" } }, "nbformat": 4, "nbformat_minor": 5 }