{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# String Operations" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello world'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"heLlo World\".capitalize() # first letter uppercase all other lower" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"hello World\".casefold() # lowercase" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' Hello World '" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World\".center(50) # put the string in center" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'xxxxxxxxxxxxxxxxxx Hello World xxxxxxxxxxxxxxxxxxx'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\" Hello World \".center(50,\"x\") #put the string in center and fill padding with x" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World Hello\".count(\"Hello\") # count freq" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World\".endswith(\"d\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello\\tWorld\".expandtabs(4) # expand \\t tabs with 4 spaces" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World\".find(\"World\") # returns index from which word's first occurance is starting otherwise -1" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World\".index(\"World\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "substring not found", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m\"Hello World\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"World\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# index and find is similar only difference is that this will throw value error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: substring not found" ] } ], "source": [ "\"Hello World\".index(\"World\",6,10) # index and find is similar only difference is that this will throw value error" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(False, True)" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World\".isalnum(),\"123\".isalnum() #alphnumeric" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(True, False)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"HelloWorld\".isalpha(),\"Hello World\".isalpha()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(True, False)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"12\".isdecimal(),\"Hello World\".isdecimal()" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(True, False)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"12\".isdigit(),\"Hello World\".isdigit()" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(True, False)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"12\".isnumeric(),\"Hello World\".isnumeric()" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(False, True)" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World\".isspace(), \" \".isspace() # if string contains only spaces" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello world ! Nishant'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\" \".join((\"hello\",\"world\",\"!\",\"Nishant\")) # join by separator" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Nishant Baheti'" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = {\"fname\":\"Nishant\",\"lname\":\"Baheti\"}\n", "\" \".join(x.values())" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World\".lower()" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HELLO WORLD'" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World\".upper()" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello world'" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = \"Jello world\".maketrans(\"J\",\"H\")\n", "\"Jello world\".translate(x)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('I could eat bananas all day, bananas ', 'bananas', ' are my favorite fruit')" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"I could eat bananas all day, bananas bananas are my favorite fruit\".rpartition(\"bananas\") # starts from last occurance" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Hello', ' world', ' Nishant']" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello, world, Nishant\".split(\",\")" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\" Hello World\".lstrip()" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World \".rstrip() " ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\" Hello World \".strip()" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hello World\".startswith(\"H\")" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'eLLO tHERE MAtE'" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Ello There maTe\".swapcase()" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'My Name Is Nishant And I Am Exploring String Methods In Python'" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"my name is nishant and i am exploring string methods in python\".title()" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'00000000000000000500'" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"500\".zfill(20)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.8.3 64-bit ('anaconda3': conda)", "language": "python", "name": "python38364bitanaconda3conda5a21e7c9305747559a413d7622491a2e" }, "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }