Xs 2 Strings in Python.ipynb
"nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "source": [ "# Introduction to Strings in Python\n", "# 1. Creating Strings" ], "metadata": { "id": "0Lm_lWtNJGWQ" } }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "cq3Py3-MI27N", "outputId": "1cfc7ad8-a93e-41ce-9116-cdb605c6b45f" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "This is a\n", "multiline string.\n" ] } ], "source": [ "s1 = 'Hello'\n", "s2 = \"World\"\n", "s3 = '''This is a\n", "multiline string.'''\n", "print(s3)\n" ] }, { "cell_type": "markdown", "source": [ "# 2. Traversing a String" ], "metadata": { "id": "IHhkV2QQI_G_" }},{ "cell_type": "code", "source": [ "text = \"Python\"\n", "print(text[-1]) # n\n", "print(text[0]) # P\n", "print(\"-\"*10)\n", "for _ in text:\n", " print(_)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "U2po4GfaI_hu", "outputId": "bca806f5-b3ec-43a8-b641-8aeaf5a924c5" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "n\n", "P\n", "----------\n", "P\n", "y\n", "t\n", "h\n", "o\n", "n\n" ] } ]},{ "cell_type": "markdown", "source": [ "# 3. Multiline Strings" ], "metadata": { "id": "NbrL8dWmJAHy" }},{ "cell_type": "code", "source": [ "para = \"\"\"This is a paragraph.\n", "It has multiple lines.\n", "Useful for documentation.\"\"\"\n", "print(para)" ], "metadata": { "id": "WUSk6bbiJAdM", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "00361862-6583-4443-8c43-16c330dcbac1" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "This is a paragraph.\n", "It has multiple lines.\n", "Useful for documentation.\n" ] } ]},{ "cell_type": "markdown", "source": [ "\n", "# 4. Concatenating Strings" ], "metadata": { "id": "BcZmlj_lJA08" }},{ "cell_type": "code", "source": [ "first = \"Hello\"\n", "last = \"World\"\n", "combined = first+ \" \"+ last\n", "print(combined) # Hello World\n", "print(first, last)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dvU04BIWJBO_", "outputId": "83721485-4c9a-4cb6-a788-2ba0ebdfdaf6" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Hello World\n", "Hello World\n" ] } ]},{ "cell_type": "markdown", "source": [ "# 5. Escape Sequences" ], "metadata": { "id": "XHke2N8CJBnv" }},{ "cell_type": "code", "source": [ "quote = \"She said, \\\"Hello!\\\"\\n Is everything okay?\"\n", "print(quote)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "HIeIzmkqJBz1", "outputId": "7bcf13a3-05df-4056-c930-b0f6dc24e386" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "She said, \"Hello!\"\n", " Is everything okay?\n" ] } ]},{ "cell_type": "markdown", "source": [ "\n", "# 6. String Operators\n" ], "metadata": { "id": "ATZrAZMcJCSI" }},{ "cell_type": "code", "source": [ "print(\"Py\" + \"thon\") # Python\n", "print(\"Hi! \" * 3) # Hi! Hi! Hi!\n", "print(\"a\" not in \"apple\") # True\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "v8kl9jO3JCyS", "outputId": "a76c6261-935d-4c2c-b60c-0a5c29e92094" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Python\n", "Hi! Hi! Hi! \n", "False\n" ] } ]},{ "cell_type": "markdown", "source": [ "# 7. String Built-in Functions\n" ], "metadata": { "id": "jIjWAm5IJDPN" }},{ "cell_type": "code", "source": [ "sample = \"hello world\"\n", "print(len(sample)) # Length\n", "print(sample.upper()) # HELLO WORLD\n", "print(sample.lower()) # hello world\n", "print(sample.endswith(\"d\")) # True\n", "print(sample.startswith(\"he\")) # True\n", "print(sample.find(\"o\",5,7)) # 4\n", "print(sample.islower()) # True\n", "print(sample.replace(sample[2], \"x\")) # hexxo worxd\n", "print(sample.count(\"l\")) # 3\n", "print(\"abc\".isalpha()) # True\n", "print(\"123\".isdigit()) # True\n", "print(\"abc123\".isalnum()) # True\n", "print(\"-\".join([\"a\", \"b\", \"c\"])) # a-b-c\n", "\n" ], "metadata": { "id": "f3ASdm9QJDg9", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "1d33a7d3-8259-4684-ee08-95428a886f9f" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "11\n", "HELLO WORLD\n", "hello world\n", "True\n", "True\n", "-1\n", "True\n", "hexxo worxd\n", "3\n", "True\n", "True\n", "True\n", "a-b-c\n" ] } ]},{ "cell_type": "markdown", "source": [ "# 8. String Slicing" ], "metadata": { "id": "aymmIE8wJDv8" }},{ "cell_type": "code", "source": [ "text = \"Python\"\n", "print(text[0:4]) # Pyth\n", "print(text[:]) # Python\n", "print(text[::-1]) # nohtyP\n", "print(text[::2]) # Pto\n" ], "metadata": { "id": "EqvYfxvFJECH", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "d4d9b07d-0f3f-4bc4-9ade-831e847324f4" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Pyth\n", "Python\n", "nohtyP\n", "Pto\n" ] } ]},{ "cell_type": "markdown", "source": [ "# 9. Practice Examples (without user input)\n", "Count vowels" ], "metadata": { "id": "kEAHi0SBJqi4" }},{ "cell_type": "code", "source": [ "vowels = 'aeiou'\n", "string = \"education\"\n", "total = 0\n", "for _ in vowels:\n", " count = string.count(_)\n", " total += count\n", "print(\"Vowel count:\", total)\n", "\n", "# total = sum(1 for c in string if c in vowels)" ], "metadata": { "id": "C2AlDEyhJq0F", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "a0e0160b-9dad-41e5-d078-82ad276a2c03" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Vowel count: 5\n" ] } ]},{ "cell_type": "markdown", "source": [ "\n", "# Palindrome check" ], "metadata": { "id": "0-VruyJKJsBa" }},{ "cell_type": "code", "source": [ "palindrome = \"madam\"\n", "print(\"Is palindrome:\", palindrome == palindrome[::-1])\n" ], "metadata": { "id": "I-43r9vZJsVO", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "968f320e-d715-4ddc-cb36-ca893993f2f3" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Is palindrome: True\n" ] } ]},{ "cell_type": "markdown", "source": [ "\n", "# Replace space with hyphens" ], "metadata": { "id": "2SxGlxs1Jsof" }},{ "cell_type": "code", "source": [ "line = \"This is Python\"\n", "print(line.replace(\" \", \"-\"))" ], "metadata": { "id": "VBQ9InDbJs9c", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "2399b086-4c1e-4c8c-bfcb-fbd72c24abc1" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "This-is-Python\n" ] } ]},{ "cell_type": "markdown", "source": [ "# Join list into string" ], "metadata": { "id": "P-_FPf3WJwEe" }},{ "cell_type": "code", "source": [ "words = [\"Code\", \"With\", \"Python\"]\n", "print(\" \".join(words))" ], "metadata": { "id": "1NdgtvYqJwWc", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "5b5ced30-6f02-4abd-e63f-44e545666b6c" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Code With Python\n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Hello\"[1:4])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "bEmkGMr5B6KE", "outputId": "33b59f25-2217-4a23-d215-af3c59cd5924" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "ell\n" ] } ] } ]}