{"id":1925,"date":"2020-12-28T01:02:15","date_gmt":"2020-12-28T01:02:15","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1925"},"modified":"2020-12-29T03:00:57","modified_gmt":"2020-12-29T03:00:57","slug":"python-string-methods","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-string-methods\/","title":{"rendered":"Python String Methods"},"content":{"rendered":"\n<p>This section provides you with useful <strong>Python string methods<\/strong> that help you manipulate strings more effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='concatenating-splitting-strings'>Concatenating &amp; Splitting strings <a href=\"#concatenating-splitting-strings\" class=\"anchor\" id=\"concatenating-splitting-strings\" title=\"Anchor for Concatenating &amp; Splitting strings\">#<\/a><\/h2>\n\n\n\n<p>These methods allow you to concatenate strings into one string and split a string into substrings effectively.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-string-methods\/python-string-join\/\">join()<\/a><\/td><td>Return a string that is the concatenation of strings in an iterable.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string-concatenation\/\" target=\"_blank\" rel=\"noreferrer noopener\">concat<\/a><\/td><td>Return a string that is the result of a concatenation of two or more strings.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string-split\/\">split()<\/a><\/td><td>Return a list of substring split from the string by a delimiter.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='finding-substrings'>Finding substrings <a href=\"#finding-substrings\" class=\"anchor\" id=\"finding-substrings\" title=\"Anchor for Finding substrings\">#<\/a><\/h2>\n\n\n\n<p>These methods allow you to locate a substring in a string and  return the index of the substring.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string-index\/\">index()<\/a>&nbsp;<\/td><td>Return the index of a substring in a string or raise a <code>ValueError<\/code> if the substring not found.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string-find\/\">find()<\/a>&nbsp;<\/td><td>Return the index of the first occurrence of a substring in a string or -1 if the substring not found.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string-startswith\/\">startswith()<\/a><\/td><td>Return True if a string starts with another string.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string-endswith\/\">endswith()<\/a>&nbsp;<\/td><td>Return True if a string ends with another string.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='dealing-with-string-cases'>Dealing with string cases <a href=\"#dealing-with-string-cases\" class=\"anchor\" id=\"dealing-with-string-cases\" title=\"Anchor for Dealing with string cases\">#<\/a><\/h2>\n\n\n\n<p>These methods help you deal with string cases effectively including lowercase, uppercase, titlecase, and casefold.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string-lower\/\">lower()<\/a><\/td><td>Return a copy of a string in lowercase<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string-upper\/\" target=\"_blank\" rel=\"noreferrer noopener\">upper()<\/a><\/td><td>Return a copy of a string in uppercase.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-titlecase\/\" target=\"_blank\" rel=\"noreferrer noopener\">title()<\/a><\/td><td>Return a copy of a string in the titlecase.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-capitalize\/\" target=\"_blank\" rel=\"noreferrer noopener\">capitalize()<\/a><\/td><td>Return a string with the first letter converted to uppercase and other letters converted to lowercase.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-string-methods\/python-string-islower\/\">islower()<\/a><\/td><td>Return True if all cased characters in the string are lowercase and there&#8217;s at least one cased character.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-string-methods\/python-string-istitle\/\">istitle()<\/a><\/td><td>Return True if the string follows the title case rules.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-string-methods\/python-string-isupper\/\">isupper()<\/a><\/td><td>Return True if all the cased characters in the string are uppercase and there&#8217;s at least one cased character.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-string-methods\/python-string-casefold\/\">casefold()<\/a><\/td><td>Return a casefolded&nbsp;copy&nbsp;of&nbsp;the&nbsp;string.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-string-methods\/python-string-swapcase\/\">swapcase()<\/a><\/td><td>Return a copy of the string with all lowercase characters converted to uppercase and vice versa<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='removing-unwanted-characters'>Removing unwanted characters <a href=\"#removing-unwanted-characters\" class=\"anchor\" id=\"removing-unwanted-characters\" title=\"Anchor for Removing unwanted characters\">#<\/a><\/h2>\n\n\n\n<p>These methods allow you to remove leading and\/or trailing unwanted characters from a string and return a new string.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string-lstrip\/\" target=\"_blank\" rel=\"noreferrer noopener\">lstrip()<\/a><\/td><td>Return a copy of a string with leading characters removed.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string-rstrip\/\">rstrip()<\/a>&nbsp;<\/td><td>Return a copy of a string with trailing characters removed.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string-strip\/\">strip()<\/a>&nbsp;<\/td><td>Return a copy of a string with leading and trailing characters removed.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='replacing-substrings'>Replacing substrings <a href=\"#replacing-substrings\" class=\"anchor\" id=\"replacing-substrings\" title=\"Anchor for Replacing substrings\">#<\/a><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-string-replace\/\">replace()<\/a>&nbsp;<\/td><td>Return a copy of a string with some or all occurrences of a substring replaced with a new one.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='checking-characters-in-strings'>Checking characters in Strings <a href=\"#checking-characters-in-strings\" class=\"anchor\" id=\"checking-characters-in-strings\" title=\"Anchor for Checking characters in Strings\">#<\/a><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-string-methods\/python-string-isdigit\/\">isdigit()<\/a><\/td><td>Return True if all characters in a string are digits.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-string-methods\/python-string-isdecimal\/\">isdecimal()<\/a><\/td><td>Return True if all characters in a string are decimal characters.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-string-methods\/python-string-isnumeric\/\">isnumeric()<\/a><\/td><td>Return True if all characters in a string are numeric characters.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-string-methods\/python-string-isalpha\/\">isalpha()<\/a><\/td><td>Return True if all characters in a string are alphabetic characters.<\/td><\/tr><tr><td><a href=\"https:\/\/www.pythontutorial.net\/python-string-methods\/python-string-isalnum\/\">isalnum()<\/a><\/td><td>Return True if all characters in a string are alphanumeric characters.<\/td><\/tr><\/tbody><\/table><\/figure>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1925\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-string-methods\/\"\n\t\t\t\tdata-post-title=\"Python String Methods\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1925\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-string-methods\/\"\n\t\t\t\tdata-post-title=\"Python String Methods\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This section provides you with useful Python string methods that help you manipulate strings more effectively.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":13,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1925","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=1925"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1925\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=1925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}