{"id":25560,"date":"2023-04-02T08:08:42","date_gmt":"2023-04-02T02:38:42","guid":{"rendered":"https:\/\/tocxten.com\/?page_id=25560"},"modified":"2023-04-02T18:27:32","modified_gmt":"2023-04-02T12:57:32","slug":"strings","status":"publish","type":"page","link":"https:\/\/tocxten.com\/index.php\/strings\/","title":{"rendered":"Strings"},"content":{"rendered":"\n<p class=\"has-medium-font-size\">In Python, a string is a sequence of characters enclosed within either single quotes (&#8216; &#8216;) or double quotes (&#8221; &#8220;). Strings are one of the basic data types in Python, and they are used to store and manipulate text-based data.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong><em>Example :<\/em><\/strong>\u00a0<strong>\u2018Hello World!\u2019<\/strong>\u00a0, \u201c<strong>Python Programming<\/strong>\u201c ,\u00a0<strong><em>\u00a0 \u201d\u2019apple<\/em><\/strong>\u201d\u2019<\/p>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#dee6ea\"><strong>Creating String<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, you can create a string by enclosing a sequence of characters within either single quotes (&#8216; &#8216;) or double quotes (&#8221; &#8220;). Here are some examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string_1 = 'This is a string created with single quotes'\r\nmy_string_2 = \"This is a string created with double quotes\"<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In these examples, <code>my_string_1<\/code> and <code>my_string_2<\/code> are both strings that contain the text &#8220;This is a string created with single quotes&#8221; and &#8220;This is a string created with double quotes&#8221;, respectively.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">You can also create strings that contain special characters such as newlines (\\n) and tabs (\\t):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string_3 = \"This string\\nhas a newline\"\r\nmy_string_4 = \"This string\\t has a tab\"<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In these examples, <code>my_string_3<\/code> contains a newline character, which causes the text &#8220;has a newline&#8221; to appear on a new line, and <code>my_string_4<\/code> contains a tab character, which causes a tab space before the text &#8220;has a tab&#8221;.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example : <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image.png\" alt=\"\" class=\"wp-image-25563\" width=\"261\" height=\"194\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image.png 372w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-300x223.png 300w\" sizes=\"auto, (max-width: 261px) 100vw, 261px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Triple quotes can extend multiple lines as illustrated below<\/strong>\u00a0:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-1.png\" alt=\"\" class=\"wp-image-25568\" width=\"363\" height=\"248\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-1.png 485w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-1-300x205.png 300w\" sizes=\"auto, (max-width: 363px) 100vw, 363px\" \/><\/figure>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#e3ebef\"><strong>Length of a string<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, you can use the built-in <code>len()<\/code> function to get the length of a string, which returns the number of characters in the string. Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"Hello, World!\"\r\nprint(len(my_string)) # Output: 13<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, the <code>len()<\/code> function is called on the <code>my_string<\/code> string, which returns the number of characters in the string (which is 13 in this case).<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note that the <code>len()<\/code> function counts all characters in the string, including spaces and special characters. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"This string\\nhas a newline\"\r\nprint(len(my_string)) # Output: 22<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, the <code>my_string<\/code> string contains a newline character (\\n), which is counted as a single character by the <code>len()<\/code> function.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">You can use the length of a string in various ways in your code, such as checking if a string is empty, looping through the characters in a string, or performing string slicing operations.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">You can find the length of a string in Python without using the built-in <code>len()<\/code> function by iterating through the characters in the string using a loop and counting them. Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"Hello, World!\"\r\ncount = 0\r\nfor char in my_string:\r\n    count += 1\r\nprint(count) # Output: 13<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, a variable <code>count<\/code> is initialized to 0, and then a <code>for<\/code> loop is used to iterate through each character in the <code>my_string<\/code> string. For each character, the <code>count<\/code> variable is incremented by 1. After the loop has finished, the <code>count<\/code> variable contains the length of the string (which is 13 in this case).<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note that this method is not as efficient as using the built-in <code>len()<\/code> function, especially for longer strings, because it involves iterating through each character in the string. However, it can be a useful exercise in understanding how strings are represented and manipulated in Python.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">One can get last letter of string using\u00a0<strong>len( )<\/strong>\u00a0function as illustrated below :<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-2.png\" alt=\"\" class=\"wp-image-25576\" width=\"243\" height=\"110\"\/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\">To get the last letter of a string in Python, you can use string indexing with a negative index of -1, which refers to the last character in the string. Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"Hello, World!\"\r\nlast_letter = my_string&#91;-1]\r\nprint(last_letter) # Output: !<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, the <code>my_string<\/code> string is indexed using a negative index of -1, which refers to the last character in the string (which is the exclamation mark in this case). The <code>last_letter<\/code> variable is then assigned this character using string indexing.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note that this method assumes that the string is not empty. If the string is empty, attempting to index it with -1 will result in an <code>IndexError<\/code>. To avoid this, you should first check if the string is non-empty before indexing it.<\/p>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#e9eff2\"><strong>String Indexing<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, you can use indexing to access individual characters in a string. String indexing is done by specifying the position of the character within square brackets (<code>[]<\/code>) immediately following the string. There are two types of indexing in Python: positive indexing and negative indexing.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"Hello, World!\"\r\nprint(my_string&#91;0]) # Output: H\r\nprint(my_string&#91;1]) # Output: e\r\nprint(my_string&#91;6]) # Output: ,<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, the <code>my_string<\/code> string is indexed using positive indices. The first <code>print<\/code> statement outputs the first character of the string (which is &#8220;H&#8221;), the second <code>print<\/code> statement outputs the second character of the string (which is &#8220;e&#8221;), and the third <code>print<\/code> statement outputs the seventh character of the string (which is &#8220;,&#8221;).<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Negative indexing, on the other hand, starts from the rightmost character of the string, with the index of the last character being -1, the second-to-last character being -2, and so on. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"Hello, World!\"\r\nprint(my_string&#91;-1]) # Output: !\r\nprint(my_string&#91;-2]) # Output: d\r\nprint(my_string&#91;-7]) # Output: <\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, the <code>my_string<\/code> string is indexed using negative indices. The first <code>print<\/code> statement outputs the last character of the string (which is &#8220;!&#8221;), the second <code>print<\/code> statement outputs the second-to-last character of the string (which is &#8220;d&#8221;), and the third <code>print<\/code> statement outputs the seventh-to-last character of the string (which is a space).<\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python string can be indexed in forward( start to end) and backward(end to start) direction as illustrated in the figure below :<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"135\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-3.png\" alt=\"\" class=\"wp-image-25587\"\/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\">Consider a string\u00a0<strong>name = \u201d Sophia<\/strong>\u201c. Here string Sophia is made up of 6 characters. Each character of the string can be accessed by using either forward indexing or backward indexing as illustrated below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-4.png\" alt=\"\" class=\"wp-image-25588\" width=\"248\" height=\"464\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-4.png 360w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-4-160x300.png 160w\" sizes=\"auto, (max-width: 248px) 100vw, 248px\" \/><\/figure>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#e4eaed\"><strong>String Traversal <\/strong><\/p>\n\n\n\n<p>String traversal in Python involves iterating over each character of a string. This can be done in both forward and reverse order using loops.Forward traversal using a while loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"hello world\"\r\ni = 0\r\nwhile i &lt; len(my_string):\r\n    print(my_string&#91;i])\r\n    i += 1\n# Output:\nh\r\ne\r\nl\r\nl\r\no\r\n\r\nw\r\no\r\nr\r\nl\r\nd<\/code><\/pre>\n\n\n\n<p>In this example, we initialize the index <code>i<\/code> to 0 and use a while loop to iterate through each character of the string. The loop continues as long as <code>i<\/code> is less than the length of the string. At each iteration, we print the character at the current index <code>i<\/code> and then increment <code>i<\/code> by 1 to move to the next character.<\/p>\n\n\n\n<p>Forward traversal using a for loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"hello world\"\r\nfor char in my_string:\r\n    print(char)\n# output\nh\r\ne\r\nl\r\nl\r\no\r\n\r\nw\r\no\r\nr\r\nl\r\nd<\/code><\/pre>\n\n\n\n<p>In this example, we use a for loop to iterate through each character of the string. The loop variable <code>char<\/code> takes on the value of each character in the string in turn, allowing us to access and print each character.<\/p>\n\n\n\n<p>Reverse traversal using a while loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"hello world\"\r\ni = len(my_string) - 1\r\nwhile i >= 0:\r\n    print(my_string&#91;i])\r\n    i -= 1\n#Output:\nd\r\nl\r\nr\r\no\r\nw\r\n\r\no\r\nl\r\nl\r\ne\r\nh<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, we initialize the index <code>i<\/code> to the last index of the string (which is the length of the string minus one) and use a while loop to iterate through each character of the string in reverse order. The loop continues as long as <code>i<\/code> is greater than or equal to 0. At each iteration, we print the character at the current index <code>i<\/code> and then decrement <code>i<\/code> by 1 to move to the previous character.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Reverse traversal using a for loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"hello world\"\r\nfor char in reversed(my_string):\r\n    print(char)\n#output\nd\r\nl\r\nr\r\no\r\nw\r\n\r\no\r\nl\r\nl\r\ne\r\nh<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, we use the built-in <code>reversed()<\/code> function to reverse the order of the characters in the string, and then use a for loop to iterate through each character in the reversed order. The loop variable <code>char<\/code> takes on the value of each character in the string in turn, allowing us to access and print each character.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Lists aren\u2019t the only data types that represent ordered sequences of values.For example, strings and lists are actually similar, if you consider a string to be a \u201clist\u201d of single text characters. Many of the things you can do with lists\u00a0 can also be done with strings:<strong><em>\u00a0indexing; slicing; and using them with for loops, with len(), and with the in and not in operators<\/em><\/strong>.<\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#dde1e3;font-size:30px\"><strong>String Slicing<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">String slicing is a technique in Python that allows you to extract a portion of a string by specifying its start and end positions. This is done using the slice operator <code>:<\/code>. The basic syntax for string slicing is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string&#91;start:end:step]<\/code><\/pre>\n\n\n\n<ul class=\"has-medium-font-size wp-block-list\"><li><code>start<\/code> is the index of the first character to include in the slice (inclusive)<\/li><li><code>end<\/code> is the index of the last character to include in the slice (exclusive)<\/li><li><code>step<\/code> is the number of characters to skip between each included character (optional)<\/li><\/ul>\n\n\n\n<p class=\"has-medium-font-size\">Here are some examples of string slicing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"Hello, World!\"\r\n\r\n# Extract the first 5 characters\r\nprint(my_string&#91;0:5])    # Output: \"Hello\"\r\n\r\n# Extract the characters from index 7 to the end\r\nprint(my_string&#91;7:])     # Output: \"World!\"\r\n\r\n# Extract the last 6 characters\r\nprint(my_string&#91;-6:])    # Output: \"World!\"\r\n\r\n# Extract every other character\r\nprint(my_string&#91;::2])    # Output: \"Hlo ol!\"\r\n\r\n# Reverse the string\r\nprint(my_string&#91;::-1])   # Output: \"!dlroW ,olleH\"<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the first example, we use the slice operator <code>0:5<\/code> to extract the first 5 characters of the string &#8220;Hello, World!&#8221;. This includes the characters at indices 0 through 4 (inclusive).<\/p>\n\n\n\n<p class=\"has-medium-font-size\">In the second example, we use the slice operator <code>7:<\/code> to extract the characters from index 7 (the &#8216;W&#8217; in &#8220;World!&#8221;) to the end of the string. This includes all characters starting at index 7 and continuing to the end of the string.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">In the third example, we use negative indexing and the slice operator <code>-6:<\/code> to extract the last 6 characters of the string &#8220;Hello, World!&#8221;. This includes all characters starting 6 positions from the end of the string and continuing to the end of the string.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">In the fourth example, we use the slice operator <code>::2<\/code> to extract every other character of the string &#8220;Hello, World!&#8221;. This includes the first character, then skips the next character, then includes the third character, and so on.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">In the fifth example, we use negative step and the slice operator <code>[::-1]<\/code> to reverse the string &#8220;Hello, World!&#8221;. This includes all characters in the string, but they are extracted in reverse order because the step is -1.<\/p>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#e3f0f5\"><strong>Example :<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-5.png\" alt=\"\" class=\"wp-image-25604\" width=\"185\" height=\"117\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>The&nbsp;<strong>:&nbsp;<\/strong>operator in slicing&nbsp;<strong>[n:m]<\/strong>&nbsp;returns the part of the string from the \u201cn-eth\u201d character to the \u201cm-eth\u201d character, including the \ufb01rst but excluding the last.<\/li><li>If you omit the \ufb01rst index (before the colon), the slice starts at the beginning of the string.<\/li><li>If you omit the second index, the slice goes to the end of the string:<ul><li>fruit = \u2018banana\u2019<\/li><li>fruit[:3]&nbsp; gives \u2018ban\u2019<\/li><li>fruit[3:]&nbsp; gives \u2018ana\u2019<\/li><\/ul><\/li><li>If the \ufb01rst index is greater than or equal to the second the result is an empty string, represented by two quotation marks:<\/li><li>fruit = \u2018banana\u2019<\/li><li>fruit[3:3]&nbsp;&nbsp;&nbsp;&nbsp; gives&nbsp; \u2018 \u2018<\/li><li>An empty string contains no characters and has length 0, but other than that, it is the same as any other string.<\/li><\/ul>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example : <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-6.png\" alt=\"\" class=\"wp-image-25605\" width=\"305\" height=\"421\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-6.png 417w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-6-217x300.png 217w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#dfe8eb;font-size:30px\"><strong>Strings are immutable<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, strings are immutable objects, which means that once a string is created, it cannot be modified. Immutable objects are objects whose state cannot be changed once they are created.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">When we modify a string in Python, what actually happens is that a new string is created with the desired modifications, while the original string remains unchanged. This is different from mutable objects like lists, where you can modify their elements in place.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here is an example to demonstrate string immutability:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"hello\"\r\nmy_string&#91;0] = \"H\"    # This will cause an error<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, we try to modify the first character of the string &#8220;hello&#8221; to be uppercase. However, this will result in a TypeError, because strings are immutable and cannot be modified in place.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Instead, we need to create a new string with the desired modifications. Here is an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"hello\"\r\nnew_string = \"H\" + my_string&#91;1:]\r\nprint(new_string)    # Output: \"Hello\"<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, we create a new string by concatenating the uppercase letter &#8220;H&#8221; with a slice of the original string that starts at index 1 and goes to the end. This creates a new string &#8220;Hello&#8221; that has the desired modification.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Example : The proper way to\u00a0<strong>\u2018mutate<\/strong>\u2018 a string is to\u00a0<strong>slice<\/strong>\u00a0and\u00a0<strong>concatenate\u00a0<\/strong>to build a new string by copying from parts of the old string as illustrated below :<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-7.png\" alt=\"\" class=\"wp-image-25612\" width=\"480\" height=\"209\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-7.png 598w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-7-300x131.png 300w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-9.png\" alt=\"\" class=\"wp-image-25616\" width=\"362\" height=\"138\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-9.png 467w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-9-300x114.png 300w\" sizes=\"auto, (max-width: 362px) 100vw, 362px\" \/><\/figure>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#dbe5ea\"><strong>Looping and Counting<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">Looping over a string: To loop over a string in Python, you can use a <code>for<\/code> loop. In each iteration of the loop, the loop variable will take on the value of the next character in the string. Here is an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"hello\"\r\nfor char in my_string:\r\n    print(char)\n# Output:\nh\r\ne\r\nl\r\nl\r\no<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">Counting occurrences of a character in a string: To count the number of occurrences of a specific character in a string, you can use the <code>count()<\/code> method of the string. This method takes a single argument, which is the character to count. Here is an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"hello\"\r\ncount = my_string.count('l')\r\nprint(count)    # Output: 2<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">Counting occurrences of a substring in a string: <\/p>\n\n\n\n<p class=\"has-medium-font-size\">To count the number of occurrences of a substring in a string, you can use a loop and the <code>count()<\/code> method. Here is an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"hello world\"\r\nsubstring = \"l\"\r\ncount = 0\r\nfor char in my_string:\r\n    if char == substring:\r\n        count += 1\r\nprint(count)    # Output: 3<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, we loop over each character in the string and increment a counter if the character matches the substring we are searching for.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example : <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-10.png\" alt=\"\" class=\"wp-image-25622\" width=\"291\" height=\"169\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-10.png 404w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-10-300x174.png 300w\" sizes=\"auto, (max-width: 291px) 100vw, 291px\" \/><\/figure>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#e1ecf0\"><strong>Example : usage of in operator for strings<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>in<\/code> operator is a built-in operator in Python that is used to check whether a given value is present in a sequence (such as a string, list, or tuple). When used with a string, the <code>in<\/code> operator checks whether a substring is present in the string. Here are some examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Check if a substring is present in a string\r\nmy_string = \"hello world\"\r\nsubstring = \"world\"\r\nif substring in my_string:\r\n    print(\"Substring found\")\r\nelse:\r\n    print(\"Substring not found\")\r\n\r\n# Check if a character is present in a string\r\nmy_string = \"hello\"\r\nchar = \"l\"\r\nif char in my_string:\r\n    print(\"Character found\")\r\nelse:\r\n    print(\"Character not found\")\n#Output\nSubstring found\r\nCharacter found<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the first example, we use the <code>in<\/code> operator to check whether the substring &#8220;world&#8221; is present in the string &#8220;hello world&#8221;. Since it is present, the output is &#8220;Substring found&#8221;.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">In the second example, we use the <code>in<\/code> operator to check whether the character &#8220;l&#8221; is present in the string &#8220;hello&#8221;. Since it is present twice, the output is &#8220;Character found&#8221;.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note that the <code>in<\/code> operator is case-sensitive. For example, if you search for the substring &#8220;World&#8221; in the string &#8220;hello world&#8221;, it will not be found because the capitalization does not match.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Example : <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-11.png\" alt=\"\" class=\"wp-image-25628\" width=\"257\" height=\"142\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-11.png 403w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-11-300x166.png 300w\" sizes=\"auto, (max-width: 257px) 100vw, 257px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#eef0f0;font-size:30px\"><strong>String Comparison using ==,&lt; and > operators<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, you can compare strings using the <code>==<\/code>, <code>&lt;<\/code>, and <code>&gt;<\/code> operators, just like you can with other data types such as numbers. Here&#8217;s how each operator works for string comparison:<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><code>==<\/code> (equal to) operator: The <code>==<\/code> operator checks whether two strings have the same content. It returns <code>True<\/code> if the strings are equal and <code>False<\/code> otherwise. Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string1 = \"hello\"\r\nstring2 = \"hello\"\r\nif string1 == string2:\r\n    print(\"The strings are equal\")\r\nelse:\r\n    print(\"The strings are not equal\")\n#Output: The strings are equal<\/code><\/pre>\n\n\n\n<p><code>&lt;<\/code> (less than) and <code>><\/code> (greater than) operators: <\/p>\n\n\n\n<p>The <code>&lt;<\/code> and <code>><\/code> operators compare two strings lexicographically (i.e., in dictionary order). They return <code>True<\/code> if the first string is less than or greater than the second string, respectively, and <code>False<\/code> otherwise. Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string1 = \"apple\"\r\nstring2 = \"banana\"\r\nif string1 &lt; string2:\r\n    print(\"string1 comes before string2 in dictionary order\")\r\nelse:\r\n    print(\"string2 comes before string1 in dictionary order\")\n\n#Output: string1 comes before string2 in dictionary order<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">Note that the comparison of strings is case-sensitive, meaning that uppercase letters come before lowercase letters in the ASCII table. Also, the <code>&lt;=<\/code> and <code>>=<\/code> operators can also be used for string comparison, which check whether the first string is less than or equal to, or greater than or equal to the second string, respectively.<\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#e4e8ea;font-size:30px\"><strong>Useful String methods<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">Strings are an example of Python objects. An object contains both data ( the actual string itself ) and methods which are effectively functions that are built into the object and are available to any instance of the object. Using the command\u00a0<strong>dir(str)<\/strong>\u00a0one can list all the methods supported by python as illustrated below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-12.png\" alt=\"\" class=\"wp-image-25634\" width=\"851\" height=\"231\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-12.png 1024w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-12-300x81.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-12-768x209.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-12-760x206.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-12-600x163.png 600w\" sizes=\"auto, (max-width: 851px) 100vw, 851px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Table : String Methods and Description<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Method<\/th><th>Description<\/th><\/tr><tr><td>capitalize()<\/td><td>Converts the first character to upper case<\/td><\/tr><tr><td>casefold()<\/td><td>Converts string into lower case<\/td><\/tr><tr><td>center()<\/td><td>Returns a centered string<\/td><\/tr><tr><td>count()<\/td><td>Returns the number of times a specified value occurs in a string<\/td><\/tr><tr><td>encode()<\/td><td>Returns an encoded version of the string<\/td><\/tr><tr><td>endswith()<\/td><td>Returns true if the string ends with the specified value<\/td><\/tr><tr><td>find()<\/td><td>Searches the string for a specified value and returns the position of where it was found<\/td><\/tr><tr><td>index()<\/td><td>Searches the string for a specified value and returns the position of where it was found<\/td><\/tr><tr><td>isalnum()<\/td><td>Returns True if all characters in the string are alphanumeric<\/td><\/tr><tr><td>isalpha()<\/td><td>Returns True if all characters in the string are in the alphabet<\/td><\/tr><tr><td>isdigit()<\/td><td>Returns True if all characters in the string are digits<\/td><\/tr><tr><td>isidentifier()<\/td><td>Returns True if the string is an identifier<\/td><\/tr><tr><td>islower()<\/td><td>Returns True if all characters in the string are lower case<\/td><\/tr><tr><td>isnumeric()<\/td><td>Returns True if all characters in the string are numeric<\/td><\/tr><tr><td>isspace()<\/td><td>Returns True if all characters in the string are whitespaces<\/td><\/tr><tr><td>istitle()<\/td><td>Returns True if the string follows the rules of a title<\/td><\/tr><tr><td>isupper()<\/td><td>Returns True if all characters in the string are upper case<\/td><\/tr><tr><td>ljust()<\/td><td>Returns a left justified version of the string<\/td><\/tr><tr><td>lower()<\/td><td>Converts a string into lower case<\/td><\/tr><tr><td>lstrip()<\/td><td>Returns a left trim version of the string<\/td><\/tr><tr><td>replace()<\/td><td>Returns a string where a specified value is replaced with a specified value<\/td><\/tr><tr><td>rjust()<\/td><td>Returns a right justified version of the string<\/td><\/tr><tr><td>rsplit()<\/td><td>Splits the string at the specified separator, and returns a list<\/td><\/tr><tr><td>rstrip()<\/td><td>Returns a right trim version of the string<\/td><\/tr><tr><td>split()<\/td><td>Splits the string at the specified separator, and returns a list<\/td><\/tr><tr><td>startswith()<\/td><td>Returns true if the string starts with the specified value<\/td><\/tr><tr><td>strip()<\/td><td>Returns a trimmed version of the string<\/td><\/tr><tr><td>swapcase()<\/td><td>Swaps cases, lower case becomes upper case and vice versa<\/td><\/tr><tr><td>title()<\/td><td>Converts the first character of each word to upper case<\/td><\/tr><tr><td>upper()<\/td><td>Converts a string into upper case<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>[Source : https:\/\/www.w3schools.com\/python\/python_ref_string.asp]<\/p>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#e5ecef\"><strong>capitalize() : Converts the first character to upper case<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>capitalize()<\/code> method is a built-in method in Python that converts the first character of a string to uppercase and leaves the rest of the string unchanged. If the first character of the string is already in uppercase, then the method has no effect on that character.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"hello world\"\r\ncapitalized = my_string.capitalize()\r\nprint(capitalized)   # Output: \"Hello world\"<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, the <code>capitalize()<\/code> method is used to convert the first character of the string <code>\"hello world\"<\/code> to uppercase, resulting in the string <code>\"Hello world\"<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note that <code>capitalize()<\/code> does not modify the original string, but returns a new string with the first character capitalized. Also, if the original string is empty, the <code>capitalize()<\/code> method will return an empty string.<\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#e5ecef\"><strong>lower()<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>lower()<\/code> method is a built-in string method in Python that returns a new string with all the alphabetic characters in the original string converted to lowercase.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"Hello, World!\"\r\nlowercase_string = my_string.lower()\r\nprint(lowercase_string)\n# Output: hello, world!<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, the <code>lower()<\/code> method is called on the <code>my_string<\/code> variable and the returned value is assigned to the <code>lowercase_string<\/code> variable. The original string <code>my_string<\/code> is not modified, instead a new string is created with all alphabetic characters in lowercase.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note that <code>lower()<\/code> method only works with alphabetic characters, and any non-alphabetic characters (such as punctuation, numbers, and spaces) are not affected by the method.<\/p>\n\n\n\n<p>Example : <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-13.png\" alt=\"\" class=\"wp-image-25646\" width=\"490\" height=\"220\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-13.png 587w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-13-300x134.png 300w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/figure>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#dfeaef\"><strong>upper()<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>upper()<\/code> method is a built-in string method in Python that is used to convert all the lowercase characters in a string to uppercase characters. The method takes no arguments and returns a new string with all the characters in uppercase.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"hello, world!\"\r\nuppercase_text = text.upper()\r\nprint(uppercase_text)\n# Output : HELLO, WORLD!<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, the <code>upper()<\/code> method is used to convert all the lowercase characters in the string <code>text<\/code> to uppercase characters. The resulting string is then stored in the variable <code>uppercase_text<\/code> and printed to the console.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">It&#8217;s important to note that the <code>upper()<\/code> method does not modify the original string, but instead returns a new string with all the characters in uppercase. If you want to modify the original string, you can assign the result of the <code>upper()<\/code> method back to the original variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"hello, world!\"\r\ntext = text.upper()\r\nprint(text)\n# Output : HELLO, WORLD!<\/code><\/pre>\n\n\n\n<p>Example :<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-14.png\" alt=\"\" class=\"wp-image-25648\" width=\"433\" height=\"190\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-14.png 594w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-14-300x132.png 300w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/figure>\n\n\n\n<p style=\"font-size:30px\"><strong>casefold(): Converts string into lower case<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">Actually, the <code>casefold()<\/code> method in Python is used to convert a string into a lowercase string, just like the <code>lower()<\/code> method. However, the <code>casefold()<\/code> method is more aggressive in its conversion and is often preferred over the <code>lower()<\/code> method when dealing with case-insensitive string comparisons.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>casefold()<\/code> method removes all case distinctions from a string and converts it into a form that is suitable for case-insensitive comparisons. This method also handles some special cases, such as the German &#8220;sharp s&#8221; character (\u00df), which is converted to &#8220;ss&#8221; when casefolded.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Hello, World!\"\r\nlowercase_text = text.casefold()\r\nprint(lowercase_text)\n#output\nhello, world!<\/code><\/pre>\n\n\n\n<p>In the above example, the <code>casefold()<\/code> method is used to convert the string <code>text<\/code> to lowercase. The resulting string is then stored in the variable <code>lowercase_text<\/code> and printed to the console.<\/p>\n\n\n\n<p>Just like the <code>lower()<\/code> method, the <code>casefold()<\/code> method does not modify the original string, but instead returns a new string with all the characters in lowercase. If you want to modify the original string, you can assign the result of the <code>casefold()<\/code> method back to the original variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Hello, World!\"\r\ntext = text.casefold()\r\nprint(text)\n# Output:\nhello, world!<\/code><\/pre>\n\n\n\n<p>Example:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-15.png\" alt=\"\" class=\"wp-image-25650\" width=\"395\" height=\"115\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-15.png 568w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-15-300x88.png 300w\" sizes=\"auto, (max-width: 395px) 100vw, 395px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>center() : Returns a centered string<\/strong><\/p>\n\n\n\n<p>The <code>center()<\/code> method is a built-in string method in Python that returns a centered string. It takes two arguments: <code>width<\/code>, which specifies the total width of the resulting string, and an optional <code>fillchar<\/code> argument that specifies the character to be used for padding.<\/p>\n\n\n\n<p>Here&#8217;s the syntax for <code>center()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string.center(width&#91;, fillchar])<\/code><\/pre>\n\n\n\n<p>The <code>width<\/code> argument is required, and must be a positive integer. The <code>fillchar<\/code> argument is optional, and must be a string of length 1. If <code>fillchar<\/code> is not specified, a space character is used for padding.<\/p>\n\n\n\n<p>Here&#8217;s an example of how to use the <code>center()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string = \"Hello\"\r\nwidth = 10\r\nfillchar = \"*\"\r\ncentered_string = string.center(width, fillchar)\r\nprint(centered_string)\n# Output:\n**Hello***<\/code><\/pre>\n\n\n\n<p>In this example, the original string <code>\"Hello\"<\/code> is centered in a string of width <code>10<\/code> with the fill character <code>*<\/code>, resulting in the string <code>**Hello***<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-16.png\" alt=\"\" class=\"wp-image-25653\" width=\"441\" height=\"127\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-16.png 536w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-16-300x86.png 300w\" sizes=\"auto, (max-width: 441px) 100vw, 441px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#d8e1e5;font-size:30px\"><strong>swapcase()<\/strong><\/p>\n\n\n\n<p><code>swapcase()<\/code> is a built-in string method in Python that returns a new string where the case of each character in the original string is swapped. In other words, all uppercase characters are converted to lowercase, and all lowercase characters are converted to uppercase.<\/p>\n\n\n\n<p>Here&#8217;s the syntax for <code>swapcase()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string.swapcase()<\/code><\/pre>\n\n\n\n<p>Here&#8217;s an example of how to use the <code>swapcase()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string = \"Hello World\"\r\nswapped_string = string.swapcase()\r\nprint(swapped_string)\n#Output: hELLO wORLD<\/code><\/pre>\n\n\n\n<p>In this example, the original string <code>\"Hello World\"<\/code> is swapped using the <code>swapcase()<\/code> method, resulting in the string <code>\"hELLO wORLD\"<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-17.png\" alt=\"\" class=\"wp-image-25661\" width=\"531\" height=\"153\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-17.png 744w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-17-300x87.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-17-600x173.png 600w\" sizes=\"auto, (max-width: 531px) 100vw, 531px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#e1edf2;font-size:30px\"><strong>find()<\/strong><\/p>\n\n\n\n<p><code>find()<\/code> is a built-in string method in Python that returns the lowest index of the first occurrence of a specified substring within a string. If the substring is not found, it returns -1.<\/p>\n\n\n\n<p>Here&#8217;s the syntax for <code>find()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string.find(substring&#91;, start&#91;, end]])<\/code><\/pre>\n\n\n\n<p>The <code>substring<\/code> argument is required, and specifies the substring to be searched within the string. The <code>start<\/code> and <code>end<\/code> arguments are optional, and specify the starting and ending index positions for the search. If <code>start<\/code> is not specified, the search starts from the beginning of the string. If <code>end<\/code> is not specified, the search goes until the end of the string.<\/p>\n\n\n\n<p>Here&#8217;s an example of how to use the <code>find()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string = \"Hello World\"\r\nsubstring = \"World\"\r\nindex = string.find(substring)\r\nprint(index)\n# output : 6<\/code><\/pre>\n\n\n\n<p>In this example, the <code>find()<\/code> method is used to search for the substring <code>\"World\"<\/code> within the string <code>\"Hello World\"<\/code>. The method returns the lowest index of the substring, which is <code>6<\/code>.<\/p>\n\n\n\n<p>If the substring is not found within the string, the <code>find()<\/code> method returns -1:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string = \"Hello World\"\r\nsubstring = \"Universe\"\r\nindex = string.find(substring)\r\nprint(index)\n# Output: -1<\/code><\/pre>\n\n\n\n<p>Example : <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1014\" height=\"265\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-18.png\" alt=\"\" class=\"wp-image-25667\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-18.png 1014w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-18-300x78.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-18-768x201.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-18-760x199.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-18-600x157.png 600w\" sizes=\"auto, (max-width: 1014px) 100vw, 1014px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"259\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-19.png\" alt=\"\" class=\"wp-image-25669\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-19.png 1024w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-19-300x76.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-19-768x194.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-19-760x192.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-19-600x152.png 600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#dfe8eb;font-size:30px\"><strong>count()<\/strong><\/p>\n\n\n\n<p><code>count()<\/code> is a built-in string method in Python that returns the number of occurrences of a specified substring within a string.Here&#8217;s the syntax for <code>count()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string.count(substring&#91;, start&#91;, end]])<\/code><\/pre>\n\n\n\n<p>The <code>substring<\/code> argument is required, and specifies the substring to be counted within the string. The <code>start<\/code> and <code>end<\/code> arguments are optional, and specify the starting and ending index positions for the count. If <code>start<\/code> is not specified, the count starts from the beginning of the string. If <code>end<\/code> is not specified, the count goes until the end of the string.Here&#8217;s an example of how to use the <code>count()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string = \"Hello World\"\r\nsubstring = \"l\"\r\ncount = string.count(substring)\r\nprint(count)\n# output : 3<\/code><\/pre>\n\n\n\n<p>In this example, the <code>count()<\/code> method is used to count the number of occurrences of the substring <code>\"l\"<\/code> within the string <code>\"Hello World\"<\/code>. The method returns the number of occurrences, which is <code>3<\/code>.<\/p>\n\n\n\n<p>You can also use the <code>count()<\/code> method to count the occurrences of a substring within a specific range of the string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string = \"Hello World\"\r\nsubstring = \"l\"\r\nstart = 3\r\nend = 8\r\ncount = string.count(substring, start, end)\r\nprint(count)\n#Output : 1<\/code><\/pre>\n\n\n\n<p>In this example, the <code>count()<\/code> method is used to count the number of occurrences of the substring <code>\"l\"<\/code> within the range of the string <code>\"lo Wo\"<\/code>, which starts from index <code>3<\/code> and ends at index <code>8<\/code>. The method returns the number of occurrences within the range, which is <code>1<\/code>.<\/p>\n\n\n\n<p>Example : <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-20.png\" alt=\"\" class=\"wp-image-25678\" width=\"693\" height=\"233\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-20.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-20-300x101.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-20-760x255.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-20-600x202.png 600w\" sizes=\"auto, (max-width: 693px) 100vw, 693px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#e1e6e8;font-size:30px\"><strong>strip(), lstrip() and rstrip()<\/strong><\/p>\n\n\n\n<p><code>strip()<\/code>, <code>lstrip()<\/code>, and <code>rstrip()<\/code> are built-in string methods in Python that remove leading and\/or trailing characters from a string. <code>strip()<\/code> removes leading and trailing whitespace characters (spaces, tabs, newlines) from a string. Here&#8217;s the syntax for <code>strip()<\/code> <code>strip()<\/code>, <code>lstrip()<\/code>, and <code>rstrip()<\/code> methods:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string.strip(&#91;characters])\nstring.lstrip(&#91;characters])\nstring.rstrip(&#91;characters])<\/code><\/pre>\n\n\n\n<p>The <code>characters<\/code> argument is optional, and specifies the characters to be removed from both ends of the string. If <code>characters<\/code> is not specified, it removes whitespace characters by default.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-21.png\" alt=\"\" class=\"wp-image-25682\" width=\"396\" height=\"175\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-21.png 608w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-21-300x133.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-21-600x265.png 600w\" sizes=\"auto, (max-width: 396px) 100vw, 396px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#dfe6ea;font-size:30px\"><strong>startswith() and endswith()<\/strong><\/p>\n\n\n\n<p><code>startswith()<\/code> and <code>endswith()<\/code> are built-in string methods in Python that are used to check whether a string starts with or ends with a specific substring, respectively.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>startswith()<\/code> returns <code>True<\/code> if the string starts with the specified substring, and <code>False<\/code> otherwise.<\/li><li><code>endswith()<\/code> returns <code>True<\/code> if the string ends with the specified substring, and <code>False<\/code> otherwise.<\/li><\/ul>\n\n\n\n<p>Here&#8217;s the syntax for <code>startswith()<\/code> and endswith() method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string.startswith(substring&#91;, start&#91;, end]])\nstring.endswith(substring&#91;, start&#91;, end]])<\/code><\/pre>\n\n\n\n<p>Example : <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-22.png\" alt=\"\" class=\"wp-image-25687\" width=\"322\" height=\"145\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-22.png 481w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-22-300x135.png 300w\" sizes=\"auto, (max-width: 322px) 100vw, 322px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#e3e9eb;font-size:30px\"><strong>split()<\/strong><\/p>\n\n\n\n<p><code>split()<\/code> is a built-in string method in Python that is used to split a string into a list of substrings based on a specified separator.<\/p>\n\n\n\n<p>Here&#8217;s the syntax for <code>split()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string.split(sep=None, maxsplit=-1)<\/code><\/pre>\n\n\n\n<p>The <code>sep<\/code> argument is optional, and specifies the separator to use for splitting the string. If <code>sep<\/code> is not specified, the default separator is whitespace.<\/p>\n\n\n\n<p>The <code>maxsplit<\/code> argument is optional, and specifies the maximum number of splits to be performed. If <code>maxsplit<\/code> is not specified or set to <code>-1<\/code>, all possible splits are performed.<\/p>\n\n\n\n<p>Here&#8217;s an example of how to use the <code>split()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string = \"Hello World\"\r\nwords = string.split()\r\nprint(words)\n# &#91;'Hello', 'World']<\/code><\/pre>\n\n\n\n<p>Example :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string = \"apple,banana,orange\"\r\nfruits = string.split(\",\")\r\nprint(fruits)\n#output: &#91;'apple', 'banana', 'orange']<\/code><\/pre>\n\n\n\n<p>Example:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"273\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-23-1024x273.png\" alt=\"\" class=\"wp-image-25696\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-23-1024x273.png 1024w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-23-300x80.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-23-768x205.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-23-1130x301.png 1130w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-23-760x203.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-23-600x160.png 600w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-23.png 1197w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Example :<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-24.png\" alt=\"\" class=\"wp-image-25698\" width=\"557\" height=\"200\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-24.png 726w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-24-300x108.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-24-600x216.png 600w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#d7dcde;font-size:30px\"><strong>join() <\/strong><\/p>\n\n\n\n<p><code>join()<\/code> is a built-in method in Python that can be used to concatenate a sequence of strings into a single string. The method takes an iterable, such as a list or tuple, and concatenates each element in the iterable into a single string with a separator between them.<\/p>\n\n\n\n<p>Here&#8217;s an example of how to use <code>join()<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>words = &#91;\"hello\", \"world\", \"!\"]\r\nseparator = \" \"\r\n\rsentence = separator.join(words)\r\nprint(sentence)\n#output: hello world !<\/code><\/pre>\n\n\n\n<p>In this example, we create a list of words and a separator string. We then use the <code>join()<\/code> method to concatenate the words in the list into a single string, with the separator between each word. The resulting string is assigned to the <code>sentence<\/code> variable and printed to the console.<\/p>\n\n\n\n<p>Note that the <code>join()<\/code> method can be used with any iterable that contains strings, not just lists. Additionally, the separator string can be any string value, including an empty string.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-25.png\" alt=\"\" class=\"wp-image-25703\" width=\"529\" height=\"157\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-25.png 703w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-25-300x89.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-25-600x178.png 600w\" sizes=\"auto, (max-width: 529px) 100vw, 529px\" \/><\/figure>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#e0e9ed\"><strong>ASCII VALUES<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-26.png\" alt=\"\" class=\"wp-image-25706\" width=\"580\" height=\"144\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-26.png 755w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-26-300x75.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-26-600x149.png 600w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-27.png\" alt=\"\" class=\"wp-image-25708\" width=\"555\" height=\"259\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-27.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-27-300x140.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-27-760x355.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-27-600x280.png 600w\" sizes=\"auto, (max-width: 555px) 100vw, 555px\" \/><\/figure>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#d8e3e8\"><strong>To print ASCII character for given ASCII Value:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"549\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-28-1024x549.png\" alt=\"\" class=\"wp-image-25711\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-28-1024x549.png 1024w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-28-300x161.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-28-768x411.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-28-1130x605.png 1130w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-28-760x407.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-28-600x321.png 600w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-28.png 1247w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#dde5e8;font-size:30px\"><strong>Python String Operators <\/strong><\/p>\n\n\n\n<p>Python provides several operators that can be used with strings. Here are some of the most commonly used string operators in Python:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>+<\/code> (concatenation operator): This operator is used to concatenate two or more strings. For example:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>string1 = \"Hello\"\r\nstring2 = \"world\"\r\nresult = string1 + \" \" + string2\r\nprint(result)\n#Output:  Hello world<\/code><\/pre>\n\n\n\n<p>2. <code>*<\/code> (repetition operator): This operator is used to repeat a string a certain number of times. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string = \"spam\"\r\nresult = string * 3\r\nprint(result)\n# output: spamspamspam<\/code><\/pre>\n\n\n\n<p>3. <code>in<\/code> and <code>not in<\/code> (membership operators): These operators are used to check if a string is present in another string. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string = \"hello world\"\r\nprint(\"hello\" in string)  # True\r\nprint(\"goodbye\" not in string)  # True<\/code><\/pre>\n\n\n\n<p>4. <code>%<\/code> (string formatting operator): This operator is used to format a string with variables. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Alice\"\r\nage = 30\r\nresult = \"My name is %s and I'm %d years old.\" % (name, age)\r\nprint(result)\n# output: My name is Alice and I'm 30 years old.<\/code><\/pre>\n\n\n\n<p>5. <code>[]<\/code> (indexing operator): This operator is used to access individual characters in a string. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string = \"hello\"\r\nprint(string&#91;0])  # 'h'\r\nprint(string&#91;-1])  # 'o'<\/code><\/pre>\n\n\n\n<p>6. Range slicing, denoted by <code>[:]<\/code>, is a way to extract a substring from a string in Python. It works by specifying the start and end indices of the substring that you want to extract. For example, consider the following string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>s = \"hello world\"\nTo extract the substring \"hello\", you can use the range slicing syntax s&#91;0:5]:\nsubstring = s&#91;0:5]\r\nprint(substring)\nOutput:\nhello\nIn this example, s&#91;0:5] specifies the range from index 0 (inclusive) to index 5 (exclusive), which corresponds to the substring \"hello\".\r\n\r\nYou can also omit the starting or ending index to specify a range that starts from the beginning or ends at the end of the string, respectively. For example, s&#91;:5] extracts the substring \"hello\", and s&#91;6:] extracts the substring \"world\".\nsubstring1 = s&#91;:5]\r\nsubstring2 = s&#91;6:]\r\nprint(substring1)\r\nprint(substring2)\n# output :\nhello\r\nworld\nRange slicing can also be used with negative indices, which count from the end of the string. For example, s&#91;-5:-1] extracts the substring \"worl\".\nsubstring = s&#91;-5:-1]\r\nprint(substring)\n# output:\nworl<\/code><\/pre>\n\n\n\n<p>Range slicing is a powerful feature in Python strings that allows you to extract substrings quickly and easily.<\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#e1e6e8;font-size:30px\"><strong>Format Operators<\/strong><\/p>\n\n\n\n<p>Format operators are used in print statement . There are two format operators :<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>%<\/strong><\/li><li><strong>format<\/strong>()<\/li><\/ol>\n\n\n\n<p>Both <code>%<\/code> and <code>format()<\/code> are string formatting operators in Python that allow you to insert values into a string.<\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#d7dde0;font-size:30px\">\u00a0<strong>% operator:<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Syntax&nbsp;<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>print(\"%s1  %s2  %s3 ------\"%(arg1,arg2,arg3,------argn)) <\/strong>\n\nHere, \ns1,s2 ,..........sn are conversion specifiers like <strong>d<\/strong>(for int ),<strong>f<\/strong>(for float),<strong>s<\/strong>(for string) ,etc. \nand \narg1,arg2....argn are variables or values.\n\nThe <code>%<\/code> operator is an older way to format strings and is based on C-style string formatting. It uses placeholders in the string, such as <code>%s<\/code> and <code>%d<\/code>, to indicate where variables should be inserted. Here's an example:\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Alice\"\r\nage = 30\r\nresult = \"My name is %s and I'm %d years old.\" % (name, age)\r\nprint(result)\n# Output:\nMy name is Alice and I'm 30 years old.<\/code><\/pre>\n\n\n\n<p>In this example, <code>%s<\/code> is a placeholder for a string value and <code>%d<\/code> is a placeholder for an integer value. The variables <code>name<\/code> and <code>age<\/code> are inserted into the string using the <code>%<\/code> operator.<\/p>\n\n\n\n<p>Example :<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-29.png\" alt=\"\" class=\"wp-image-25737\" width=\"546\" height=\"165\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-29.png 700w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-29-300x91.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-29-600x182.png 600w\" sizes=\"auto, (max-width: 546px) 100vw, 546px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#d7dde0;font-size:30px\"><strong>format() function<\/strong><\/p>\n\n\n\n<p><strong>Syntax :<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print('{0} {1} ......{n}'.format(agr1,agr2,agr3....argn)\nHere,\n    0,1,2,------are position specifiers  \nand \n    arg1,arg2,-----argn are variables\/values<\/pre>\n\n\n\n<p>The <code>format()<\/code> method is a newer way to format strings and is more versatile than the <code>%<\/code> operator. It uses curly braces <code>{}<\/code> to indicate where variables should be inserted. Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Alice\"\r\nage = 30\r\nresult = \"My name is {} and I'm {} years old.\".format(name, age)\r\nprint(result)\n#\nMy name is Alice and I'm 30 years old.<\/code><\/pre>\n\n\n\n<p><strong>Example :<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-30.png\" alt=\"\" class=\"wp-image-25743\" width=\"533\" height=\"139\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-30.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-30-300x78.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-30-760x198.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-30-600x156.png 600w\" sizes=\"auto, (max-width: 533px) 100vw, 533px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#d8e3e8;font-size:30px\"><strong>Sample Programs<\/strong>:<\/p>\n\n\n\n<p>1.<strong>Write a Python Program to Check if the String is Symmetrical or Palindrome<\/strong>:<\/p>\n\n\n\n<p>A\u00a0string\u00a0is said to be symmetrical if both halves of the string are the same, and a string is said to be a palindrome if one half of the string is the opposite of the other half or if the string appears the same whether read forward or backward.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-31.png\" alt=\"\" class=\"wp-image-25747\" width=\"507\" height=\"453\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-31.png 683w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-31-300x268.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-31-600x536.png 600w\" sizes=\"auto, (max-width: 507px) 100vw, 507px\" \/><\/figure>\n\n\n\n<p><strong>Output 1 :<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-32.png\" alt=\"\" class=\"wp-image-25749\" width=\"335\" height=\"70\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-32.png 467w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-32-300x62.png 300w\" sizes=\"auto, (max-width: 335px) 100vw, 335px\" \/><\/figure>\n\n\n\n<p><strong>Output 2<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-33.png\" alt=\"\" class=\"wp-image-25750\" width=\"362\" height=\"70\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-33.png 491w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-33-300x58.png 300w\" sizes=\"auto, (max-width: 362px) 100vw, 362px\" \/><\/figure>\n\n\n\n<p><strong>2. Write a Python Program to find the length of string<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"661\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-34-1024x661.png\" alt=\"\" class=\"wp-image-25751\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-34-1024x661.png 1024w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-34-300x194.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-34-768x496.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-34-760x491.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-34-600x387.png 600w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-34.png 1066w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>Output :<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"122\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-35.png\" alt=\"\" class=\"wp-image-25753\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-35.png 739w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-35-300x50.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-35-600x99.png 600w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/figure>\n\n\n\n<p><strong>3. Write a Python Program to Count the Number of Digits, Alphabets, and Other Characters in a String<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-36.png\" alt=\"\" class=\"wp-image-25755\" width=\"652\" height=\"369\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-36.png 936w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-36-300x170.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-36-768x436.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-36-760x431.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-36-600x340.png 600w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/figure>\n\n\n\n<p><strong>Output :<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-37.png\" alt=\"\" class=\"wp-image-25758\" width=\"575\" height=\"77\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-37.png 684w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-37-300x40.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-37-600x81.png 600w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/figure>\n\n\n\n<p><strong>4. Write a Python Program to Count the Number of Vowels in a String<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-38.png\" alt=\"\" class=\"wp-image-25762\" width=\"643\" height=\"363\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-38.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-38-300x169.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-38-760x428.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-38-600x338.png 600w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/figure>\n\n\n\n<p><strong>5. Write a Python Program to Split and Join a String<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-39.png\" alt=\"\" class=\"wp-image-25763\" width=\"575\" height=\"474\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-39.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-39-300x247.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-39-760x626.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-39-600x495.png 600w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/figure>\n\n\n\n<p><strong>6. Write a Python program to display a largest word from a string<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-40.png\" alt=\"\" class=\"wp-image-25764\" width=\"271\" height=\"198\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-40.png 359w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-40-300x219.png 300w\" sizes=\"auto, (max-width: 271px) 100vw, 271px\" \/><\/figure>\n\n\n\n<p><strong>7. Write a Python program to display unique words from a string<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-41.png\" alt=\"\" class=\"wp-image-25766\" width=\"348\" height=\"230\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-41.png 455w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-41-300x198.png 300w\" sizes=\"auto, (max-width: 348px) 100vw, 348px\" \/><\/figure>\n\n\n\n<p>8.<strong>Write a Program to accept a word and display a ASCII value of each character of words<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"628\" height=\"505\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-42.png\" alt=\"\" class=\"wp-image-25768\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-42.png 628w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-42-300x241.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/04\/image-42-600x482.png 600w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Questions for Practice<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li>Define String? Explain at least 5 String functions with examples.<\/li><li>Write a python program to display presence of given substring in main string.<\/li><li>Define Tuple , List , Strings and Dictionary in Python.<\/li><li>List any six methods associated with string and explain each of them with example.<\/li><li>Write a Python program to swap cases of a given string .<ul><li>Input : Java<\/li><li>Output : jAVA<\/li><\/ul><\/li><li>Explain the various string methods for the following operations with examples.<ul><li>Removing white space characters from the beginning end or both sides of a string<\/li><li>To right -justify , left justify and center a string<\/li><\/ul><\/li><li>What are escape characters?<\/li><li>What do the \\n and \\t escape characters represent?<\/li><li>How can you put a \\ backslash character in a string?<\/li><li>The string value \u201cHowl\u2019s Moving Castle\u201d is a valid string. Why isn\u2019t it<br>a problem that the single quote character in the word Howl\u2019s isn\u2019t<br>escaped?<\/li><li>If you don\u2019t want to put \\n in your string, how can you write a string<br>with newlines in it?<\/li><li>What do the following expressions evaluate to?<br>\u2022 \u2018Hello world!'[1]<br>\u2022 \u2018Hello world!'[0:5]<br>\u2022 \u2018Hello world!'[:5]<br>\u2022 \u2018Hello world!'[3:]<\/li><li>What do the following expressions evaluate to?<ul><li>\u2022 \u2018Hello\u2019.upper()<\/li><li>\u2022 \u2018Hello\u2019.upper().isupper()<\/li><li>\u2022 \u2018Hello\u2019.upper().lower()<\/li><\/ul><\/li><li>What do the following expressions evaluate to?<ul><li>\u2022 \u2018Remember, remember, the fifth of November.\u2019.split()<\/li><li>\u2022 \u2018-\u2018.join(\u2018There can be only one.\u2019.split())<\/li><\/ul><\/li><li>What string methods can you use to right-justify, left-justify, and center a string?<\/li><li>How can you trim whitespace characters from the beginning or end of<br>a string?<\/li><li>Write a Python program to calculate the length of a string.<\/li><li>Write a program to check if the letter \u2018e\u2019 is present in the word \u2018Umbrella\u2019.<\/li><li>Write a program to check if the word \u2018orange\u2019 is present in the \u201cThis is orange juice\u201d.<\/li><li>Write a program to find the first and the last occurence of the letter \u2018o\u2019 and character \u2018,\u2019 in \u201cHello, World\u201d.<\/li><li>Write a program that takes your full name as input and displays the abbreviations of the first and middle names<\/li><li>except the last name which is displayed as it is. For example, if your name is Robert Brett Roser, then the output should be R.B.Roser.<\/li><li>Write a program to find the number of vowels, consonents, digits and white space characters in a string.<\/li><li>Write a program to make a new string with all the consonents deleted from the string \u201cHello, have a good day\u201d<\/li><li>Write a Python program to count the number of characters (character frequency) in a string.<\/li><li>Write the string after the first occurrence of \u2018,\u2019 and the string after the last occurrence of \u2018,\u2019 in the string \u201cHello, Good, Morning\u201d. World\u201d.<\/li><li>Write a program to print every character of a string entered by user in a new line using loop.<\/li><li>Write a program to find the length of the string \u201crefrigerator\u201d without using len function.<\/li><li>Write a program to check if the letter \u2018e\u2019 is present in the word \u2018Umbrella\u2019.<\/li><li>Write a program to check if the word \u2018orange\u2019 is present in the \u201cThis is orange juice\u201d.<\/li><li>Write a program to find the first and the last occurence of the letter \u2018o\u2019 and character \u2018,\u2019 in \u201cHello, World\u201d.<\/li><li>Write a program that takes your full name as input and displays the abbreviations of the first and middle names except the last name which is displayed as it is. For example, if your name is Robert Brett Roser, then the output should be R.B.Roser.<\/li><li>Check the occurrence of the letter \u2018e\u2019 and the word \u2018is\u2019 in the sentence \u201cThis is umbrella\u201d.<\/li><li>Write a program to find the number of vowels, consonents, digits and white space characters in a string.<\/li><li>Write a program to make a new string with all the consonents deleted from the string \u201cHello, have a good day\u201d.<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In Python, a string is a sequence of characters enclosed within either single quotes (&#8216; &#8216;) or double quotes (&#8221; &#8220;). Strings are one of the basic data types in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-25560","page","type-page","status-publish","hentry"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/25560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/comments?post=25560"}],"version-history":[{"count":105,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/25560\/revisions"}],"predecessor-version":[{"id":25775,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/25560\/revisions\/25775"}],"wp:attachment":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/media?parent=25560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}