{"id":648,"date":"2021-02-26T13:17:48","date_gmt":"2021-02-26T07:47:48","guid":{"rendered":"https:\/\/cbsepython.in\/?p=648"},"modified":"2023-12-28T09:57:49","modified_gmt":"2023-12-28T04:27:49","slug":"string-data-type-in-python","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/string-data-type-in-python\/","title":{"rendered":"String Data Type in python Class 11 Notes"},"content":{"rendered":"<h2>String Data Type in python<\/h2>\n<p><strong>WHAT ARE STRINGS?<\/strong><\/p>\n<p>In Python, a string is a sequence of characters, enclosed in either single quotes (&#8221;) or double quotes (&#8220;&#8221;). Strings are immutable, which means once a string is created, its contents cannot be changed.<\/p>\n<p>Here is an example of a string:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"<\/pre>\n<p>You can access individual characters within a string using indexing. The index of the first character in a string is 0, and you can count backwards from the end of the string using negative indices. Here are some examples:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">first_char = my_string[0]      # returns 'H'\r\nlast_char = my_string[-1]      # returns '!'\r\n<\/pre>\n<p>You can also slice a string to extract a substring. Slicing is done using the colon (:) operator. The syntax for slicing is <code>[start_index:end_index]<\/code>, where <code>start_index<\/code> is the index of the first character you want to include in the substring, and <code>end_index<\/code> is the index of the first character you want to exclude from the substring. If you omit <code>start_index<\/code>, Python assumes you want to start at the beginning of the string, and if you omit <code>end_index<\/code>, Python assumes you want to go to the end of the string. Here are some examples:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">substring = my_string[0:5]    # returns 'Hello'\r\nsubstring2 = my_string[7:]    # returns 'World!'\r\n<\/pre>\n<p>You can also concatenate two or more strings using the <code>+<\/code> operator. Here is an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">greeting = \"Hello\"\r\nname = \"Alice\"\r\nfull_greeting = greeting + \", \" + name + \"!\"\r\n<\/pre>\n<p>This would set <code>full_greeting<\/code> to <code>\"Hello, Alice!\"<\/code>.<\/p>\n<p>here are some more operations and methods you can perform on strings in Python:<\/p>\n<ul>\n<li><strong>len(string)<\/strong>: returns the length of the string<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\nlength = len(my_string)   # returns 13\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>string.upper()<\/strong>: returns a new string with all characters in uppercase<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\nuppercase_string = my_string.upper()   # returns \"HELLO, WORLD!\"\r\n<\/pre>\n<p><strong>string.lower()<\/strong>: returns a new string with all characters in lowercase<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\nlowercase_string = my_string.lower()   # returns \"hello, world!\"\r\n<\/pre>\n<p><strong>string.strip()<\/strong>: returns a new string with leading and trailing whitespace removed<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"  Hello, World!  \"\r\nstripped_string = my_string.strip()   # returns \"Hello, World!\"\r\n<\/pre>\n<p><strong>string.replace(old, new)<\/strong>: returns a new string with all occurrences of <code>old<\/code> replaced by <code>new<\/code><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\nnew_string = my_string.replace(\"World\", \"Python\")   # returns \"Hello, Python!\"\r\n<\/pre>\n<p><strong>string.split(separator)<\/strong>: returns a list of substrings, split by the specified separator (default is whitespace)<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\nwords = my_string.split(\",\")   # returns [\"Hello\", \" World!\"]\r\n<\/pre>\n<p><strong>string.join(iterable)<\/strong>: returns a new string by concatenating the elements of an iterable, separated by the string<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">words = [\"Hello\", \"World\", \"!\"]\r\nmy_string = \", \".join(words)   # returns \"Hello, World, !\"\r\n<\/pre>\n<p><strong>string.format()<\/strong>: allows you to insert values into a string using placeholders<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">name = \"Alice\"\r\nage = 30\r\ngreeting = \"Hello, my name is {} and I am {} years old.\".format(name, age)\r\n<\/pre>\n<div class=\"group w-full text-gray-800 dark:text-gray-100 border-b border-black\/10 dark:border-gray-900\/50 bg-gray-50 dark:bg-[#444654]\">\n<div class=\"text-base gap-4 md:gap-6 md:max-w-2xl lg:max-w-xl xl:max-w-3xl p-4 md:py-6 flex lg:px-0 m-auto\">\n<div class=\"relative flex w-[calc(100%-50px)] flex-col gap-1 md:gap-3 lg:w-[calc(100%-115px)]\">\n<div class=\"flex flex-grow flex-col gap-3\">\n<div class=\"min-h-[20px] flex flex-col items-start gap-4 whitespace-pre-wrap break-words\">\n<div class=\"markdown prose w-full break-words dark:prose-invert light\">\n<p>This would set <code>greeting<\/code> to <code>\"Hello, my name is Alice and I am 30 years old.\"<\/code>. You can also use numbered placeholders to specify the order of the values in the format method, or use named placeholders to give them descriptive names.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p>Here are some additional operations and methods you can use with strings in Python:<\/p>\n<ul>\n<li><strong>string.startswith(prefix)<\/strong>: returns <code>True<\/code> if the string starts with the specified prefix, <code>False<\/code> otherwise.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\nstarts_with_hello = my_string.startswith(\"Hello\")   # returns True\r\n<\/pre>\n<\/div>\n<p><strong>string.endswith(suffix)<\/strong>: returns <code>True<\/code> if the string ends with the specified suffix, <code>False<\/code> otherwise.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\nends_with_exclamation = my_string.endswith(\"!\")   # returns True\r\n<\/pre>\n<\/div>\n<p><strong>string.find(substring)<\/strong>: returns the index of the first occurrence of the specified substring in the string, or -1 if the substring is not found.<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\nindex_of_world = my_string.find(\"World\")   # returns 7\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>string.count(substring)<\/strong>: returns the number of times the specified substring appears in the string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\nnumber_of_l = my_string.count(\"l\")   # returns 3\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>string.isalpha()<\/strong>: returns <code>True<\/code> if all characters in the string are alphabetic, <code>False<\/code> otherwise.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\nis_alpha = my_string.isalpha()   # returns False (because of the comma and the space)\r\n<\/pre>\n<p><strong>string.isdigit()<\/strong>: returns <code>True<\/code> if all characters in the string are digits, <code>False<\/code> otherwise.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"12345\"\r\nis_digit = my_string.isdigit()   # returns True\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>string.islower()<\/strong>: returns <code>True<\/code> if all alphabetic characters in the string are lowercase, <code>False<\/code> otherwise.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"hello, world!\"\r\nis_lower = my_string.islower()   # returns True\r\n<\/pre>\n<p><strong>string.isupper()<\/strong>: returns <code>True<\/code> if all alphabetic characters in the string are uppercase, <code>False<\/code> otherwise.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"HELLO, WORLD!\"\r\nis_upper = my_string.isupper()   # returns True\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>string.encode()<\/strong>: returns a bytes object encoding the string using the specified encoding (default is UTF-8)<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\nencoded_bytes = my_string.encode()   # returns b'Hello, World!'\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>string.center(width)<\/strong>: returns a new string centered within a string of a specified width, with any extra space filled by a specified character (default is whitespace).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\ncentered_string = my_string.center(20, \"*\")   # returns \"***Hello, World!***\"\r\n<\/pre>\n<p><strong>string.ljust(width)<\/strong>: returns a new string left-justified within a string of a specified width, with any extra space filled by a specified character (default is whitespace).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\nleft_justified_string = my_string.ljust(20, \"*\")   # returns \"Hello, World!*******\"\r\n<\/pre>\n<p><strong>string.rjust(width)<\/strong>: returns a new string right-justified within a string of a specified width, with any extra space filled by a specified character (default is whitespace).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\nright_justified_string = my_string.rjust(20, \"*\")   # returns \"*******Hello, World!\"\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>string.swapcase()<\/strong>: returns a new string with uppercase characters converted to lowercase and vice versa.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"Hello, World!\"\r\nswapped_case_string = my_string.swapcase()   # returns \"hELLO, wORLD!\"\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>string.title()<\/strong>: returns a new string with the first letter of each word capitalized.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"hello, world!\"\r\ntitle_string = my_string.title()   # returns \"Hello, World!\"\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>string.capitalize()<\/strong>: returns a new string with the first character capitalized and the rest in lowercase.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">my_string = \"hello, world!\"\r\ncapitalized_string = my_string.capitalize()   # returns \"Hello, world!\"\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>String Data Type in python WHAT ARE STRINGS? In Python, a string is a sequence of characters, enclosed in either single quotes (&#8221;) or double quotes (&#8220;&#8221;). Strings are immutable, which means once a string is created, its contents cannot be changed. Here is an example of a string: my_string = &#8220;Hello, World!&#8221; You can [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-648","post","type-post","status-publish","format-standard","hentry","category-cbse-computer-science-with-python-class-12"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=648"}],"version-history":[{"count":0,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/648\/revisions"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}