{"id":103,"date":"2019-02-01T01:43:02","date_gmt":"2019-02-01T01:43:02","guid":{"rendered":"https:\/\/kindsonthegenius.com\/python\/?p=103"},"modified":"2019-03-02T03:53:03","modified_gmt":"2019-03-02T03:53:03","slug":"09-python-strings","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/","title":{"rendered":"Python &#8211; Strings"},"content":{"rendered":"<p>We would cover strings under the following topics:<\/p>\n<ol>\n<li><a href=\"#t1\">Introduction<\/a><\/li>\n<li><a href=\"#t2\">Accessing String Values<\/a><\/li>\n<li><a href=\"#t3\">Escape Characters<\/a><\/li>\n<li><a href=\"#t4\">Special String Operators<\/a><\/li>\n<li><a href=\"#t5\">Formatting a String<\/a><\/li>\n<li><a href=\"#t6\">Multi-line Strings<\/a><\/li>\n<li><a href=\"#t7\">Unicode Characters<\/a><\/li>\n<li><a href=\"#t8\">String Methods<\/a><\/li>\n<li><a href=\"#t9\">Watch the Video<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Introduction<\/strong><\/h4>\n<p>A string in Python is a sequence characters enclosed in quotes. However, both single and double quotes are treated the same in Python.<\/p>\n<p>As you know, you don&#8217;t have to declare a string variable before using it. You simply need to assign a string to a variable. For example, the following statements creates strings:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">channel <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Python Programming Tutorials\"<\/span>\r\nname <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Kindson The Genius\"<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Accessing String Values<\/strong><\/h4>\n<p>It is important to note that Python does not have character type. It treats characters just the same a strings. So a single character is considered to be a string of length 1.<\/p>\n<p>You can access string and also part of a string using the index.\u00a0 The index begins from 0 (the first character in the string) to the length of the string.<\/p>\n<p>Let&#8217;s take for example:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">channel <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Python Programming Tutorials\"<\/span>\r\nname <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Kindson The Genius\"<\/span>\r\n\r\nstr1 <span style=\"color: #333333;\">=<\/span> channel[<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>]\r\nfirstname <span style=\"color: #333333;\">=<\/span> name[<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>:<span style=\"color: #0000dd; font-weight: bold;\">7<\/span>]\r\n\r\n<span style=\"color: #007020;\">print<\/span>(str1)\r\n<span style=\"color: #007020;\">print<\/span>(firstname)<span style=\"background-color: #fff0f0;\">\"<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If the above program executes, then the variable str1 would contain the letter P. Also, the variable firstname would contain the string Kindson.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">\u00a03.Escape Characters<\/strong><\/h4>\n<p>Escape characters are characters used for formatting a string output. They are also called non-printable characters. Escape characters are represented using backslash notation.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>Notation<\/th>\n<th>Hex character<\/th>\n<th class=\"ts\">What it means<\/th>\n<\/tr>\n<tr>\n<td>\\a<\/td>\n<td>0x07<\/td>\n<td>Bell or alert<\/td>\n<\/tr>\n<tr>\n<td>\\b<\/td>\n<td>0x08<\/td>\n<td>Backspace<\/td>\n<\/tr>\n<tr>\n<td>\\cx<\/td>\n<td><\/td>\n<td>Control-x<\/td>\n<\/tr>\n<tr>\n<td>\\C-x<\/td>\n<td><\/td>\n<td>Control-x<\/td>\n<\/tr>\n<tr>\n<td>\\e<\/td>\n<td>0x1b<\/td>\n<td>Escape<\/td>\n<\/tr>\n<tr>\n<td>\\f<\/td>\n<td>0x0c<\/td>\n<td>Formfeed<\/td>\n<\/tr>\n<tr>\n<td>\\M-\\C-x<\/td>\n<td><\/td>\n<td>Meta-Control-x<\/td>\n<\/tr>\n<tr>\n<td>\\n<\/td>\n<td>0x0a<\/td>\n<td>Newline character<\/td>\n<\/tr>\n<tr>\n<td>\\nnn<\/td>\n<td><\/td>\n<td>Octal notation, where n is in the range 0.7<\/td>\n<\/tr>\n<tr>\n<td>\\r<\/td>\n<td>0x0d<\/td>\n<td>Carriage return<\/td>\n<\/tr>\n<tr>\n<td>\\s<\/td>\n<td>0x20<\/td>\n<td>Space<\/td>\n<\/tr>\n<tr>\n<td>\\t<\/td>\n<td>0x09<\/td>\n<td>Tab<\/td>\n<\/tr>\n<tr>\n<td>\\v<\/td>\n<td>0x0b<\/td>\n<td>Vertical tab<\/td>\n<\/tr>\n<tr>\n<td>\\x<\/td>\n<td><\/td>\n<td>Character x<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">\\xnn<\/td>\n<td><\/td>\n<td>Hexadecimal notation. n is in the range from 0.9, a.f, or A.F<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Special String Operators<\/strong><\/h4>\n<p>There are some operators that can be used to perform certain operations in a string. For example, joining two strings together or splitting a string into parts. Or maybe retrieving part of a string. The table below gives a list of these operators and their description.<\/p>\n<p>Assuming a = &#8220;Hello&#8221; and b = &#8220;Python&#8221;<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>Operator<\/th>\n<th>Operation description<\/th>\n<th>For example<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">+<\/td>\n<td>Concatenation &#8211; Adds two strings togeterh<\/td>\n<td class=\"ts\">a + b will give HelloPython<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">*<\/td>\n<td>Repetition &#8211; Creates new strings, by joining multiple copies of the same string<\/td>\n<td class=\"ts\">a*2 will give -HelloHello<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">[]<\/td>\n<td>Slice &#8211; Produces the character from the specified index<\/td>\n<td class=\"ts\">a[1] will give e<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">[ : ]<\/td>\n<td>Range Slice &#8211; Gives the characters from the specified range<\/td>\n<td class=\"ts\">a[1:4] will give ell<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">in<\/td>\n<td>Membership &#8211; Returns true if a character exists in the specified string<\/td>\n<td class=\"ts\">H in a will give 1<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">not in<\/td>\n<td>Membership &#8211; Returns true if a character does not exist in the specified string<\/td>\n<td class=\"ts\">M not in a will give 1<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">r\/R<\/td>\n<td>Raw String &#8211; Suppresses actual meaning of the Escape characters. The syntax for raw strings is exactly the same as for normal strings with the exception of the raw string operator, the letter &#8220;r,&#8221; which precedes the quotation marks. The &#8220;r&#8221; can be lowercase (r) or uppercase (R) and must be placed immediately before the opening quotation mark.<\/td>\n<td class=\"ts\">print r&#8217;\\n&#8217; prints \\n and print R&#8217;\\n&#8217;prints \\n<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">%<\/td>\n<td>Format &#8211; Performs formatting on a string<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Formatting a String<\/strong><\/h4>\n<p>A string can be formatted using the formatting operator, %. It kind of allows you to get the behavior of the printf() function used in C programming language.<\/p>\n<p>To use it, you place the formatter inside a string and then specify the string that should be in place of the formatter. For example:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"My first car was %s. I used if for %d years\"<\/span> <span style=\"color: #333333;\">%<\/span>(<span style=\"background-color: #fff0f0;\">\"Camry\"<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">6<\/span>))\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If the above line executes, then the output will be:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">My first car <span style=\"color: #000000; font-weight: bold;\">is<\/span> Camry<span style=\"color: #333333;\">.<\/span> I used <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #0000dd; font-weight: bold;\">6<\/span> years\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The complete list of string formatters are given in the table below:<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>Formatter<\/th>\n<th>Converts to<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">%c<\/td>\n<td>character<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">%s<\/td>\n<td>string conversion via str() prior to formatting<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">%i<\/td>\n<td>signed decimal integer<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">%d<\/td>\n<td>signed decimal integer<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">%u<\/td>\n<td>unsigned decimal integer<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">%o<\/td>\n<td>octal integer<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">%x<\/td>\n<td>hexadecimal integer (lowercase letters)<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">%X<\/td>\n<td>hexadecimal integer (UPPERcase letters)<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">%e<\/td>\n<td>exponential notation (with lowercase &#8216;e&#8217;)<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">%E<\/td>\n<td>exponential notation (with UPPERcase &#8216;E&#8217;)<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">%f<\/td>\n<td>floating point real number<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">%g<\/td>\n<td>the shorter of %f and %e<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">%G<\/td>\n<td>the shorter of %f and %E<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Other symbols that could also be used are given below. I recommend you try each of them to see how they work.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>Symbol<\/th>\n<th>What is does<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">*<\/td>\n<td>argument specifies width or precision.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">&#8211;<\/td>\n<td>left justification.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">+<\/td>\n<td>display the sign.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">&lt;sp&gt;<\/td>\n<td>leaves a blank space before a positive number.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">#<\/td>\n<td>adds the octal leading zero ( &#8216;0&#8217; ) or hexadecimal leading &#8216;0x&#8217; or &#8216;0X&#8217;, depending on whether &#8216;x&#8217; or &#8216;X&#8217; were used.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">0<\/td>\n<td>pad from left with zeros<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">%<\/td>\n<td>&#8216;%%&#8217; leaves you with a single string literal &#8216;%&#8217;<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">(var)<\/td>\n<td>mapping variable (dictionary arguments)<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">m.n.<\/td>\n<td>m is the minimum total width and n is the number of digits to display after the decimal point<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4><b id=\"t6\">6. Multi-line<\/b><strong>\u00a0Strings<\/strong><\/h4>\n<p>Python also supports multi-line strings. This achieved using triple quotation marks. For example:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">text <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"\"\"It is very good for you to learn programming. <\/span>\r\n<span style=\"background-color: #fff0f0;\">I also think that starting with Python is probably the<\/span>\r\n<span style=\"background-color: #fff0f0;\">easiest way to achieve it. The remember to watch the <\/span>\r\n<span style=\"background-color: #fff0f0;\">videos\"\"\"<\/span>\r\n<\/pre>\n<p>Also, you can place escape characters inside the multiline string. For example, you may want to force a new line, in that case you simple add \\n inside the string. Try to watch the video to see how all these works.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t7\">7. Unicode String<\/strong><\/h4>\n<p>A normal Python string is stored in memory as 8-bit ASCII code. While unicode strings are stored as 16-bit unicode. The benefit of this is to allow a wider range of character set. For example some foreign language characters or scripts. (eg Chinese!)<\/p>\n<p>to create a unicode string, simply place letter u before the string. This is shown below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">ustr <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">u'Python Programing'<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t8\">8. String Method<\/strong><\/h4>\n<p>It&#8217;s important for you to know how to manipulate strings. This you can do using the string methods. A table of string methods and description is given below.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>SN<\/th>\n<th>Methods and brief description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><strong>capitalize()<\/strong>Capitalizes first letter of particular string<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><strong>center(width, fillchar)<\/strong>Returns a space-padded string with the original string centered to a total of width columns.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><strong>count(str, beg= 0,end=len(string))<\/strong>Counts how many times str occurs in string or in a substring of string if starting index beg and ending index end are given.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><strong>decode(encoding=&#8217;UTF-8&#8242;,errors=&#8217;strict&#8217;)<\/strong>Decodes the string using the codec registered for encoding. encoding defaults to the default string encoding.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><strong>encode(encoding=&#8217;UTF-8&#8242;,errors=&#8217;strict&#8217;)<\/strong>Returns encoded string version of string; on error, default is to raise a ValueError unless errors is given with &#8216;ignore&#8217; or &#8216;replace&#8217;.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">6<\/td>\n<td><strong>endswith(suffix, beg=0, end=len(string))<\/strong>Checks if string or a substring of string (if starting index beg and ending index end are given) ends with suffix; returns true if yes. Returns false if otherwise<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">7<\/td>\n<td><strong>expandtabs(tabsize=8)<\/strong>Expands tabs in string to multiple spaces; defaults to 8 spaces per tab if tabsize not provided.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">8<\/td>\n<td><strong>find(str, beg=0 end=len(string))<\/strong>Determine if str occurs in string or in a substring of string if starting index beg and ending index end are given returns index if found and -1 otherwise.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">9<\/td>\n<td><strong>index(str, beg=0, end=len(string))<\/strong>Same as find(), but raises an exception if str not found.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">10<\/td>\n<td><strong>isalnum()<\/strong>Returns true if string has at least 1 character and all characters are alphanumeric and false otherwise.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">11<\/td>\n<td><strong>isalpha()<\/strong>Returns true if string has at least 1 character and all characters are alphabetic and false otherwise.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">12<\/td>\n<td><strong>isdigit()<\/strong>Returns true if string contains only digits and false otherwise.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">13<\/td>\n<td><strong>islower()<\/strong>Returns true if string has at least 1 cased character and all cased characters are in lowercase and false otherwise.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">14<\/td>\n<td><strong>isnumeric()<\/strong>Returns true if a unicode string contains only numeric characters. It returns false if otherwise.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">15<\/td>\n<td><strong>isspace()<\/strong>Returns true if string contains only whitespace characters. It returns false if otherwise.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">16<\/td>\n<td><strong>istitle()<\/strong>Returns true if string is properly &#8220;titlecased&#8221; and false otherwise.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">17<\/td>\n<td><strong>isupper()<\/strong>Returns true if string has at least one cased character and all cased characters are in uppercase. It returns if otherwise.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">18<\/td>\n<td><strong>join(seq)<\/strong>Merges the string representations of elements in sequence seq into a string, with separator string.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">19<\/td>\n<td><strong>len(string)<\/strong>Returns the length of the string. That is number of characters in the string<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">20<\/td>\n<td><strong>ljust(width[, fillchar])<\/strong>Returns a space-padded string with the original string left-justified to a total of width columns.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">21<\/td>\n<td><strong>lower()<\/strong>Converts all uppercase letters in string to lowercase.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">22<\/td>\n<td><strong>lstrip()<\/strong>Removes all leading whitespace in string.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">23<\/td>\n<td><strong>maketrans()<\/strong>Returns a translation table to be used in translate function.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">24<\/td>\n<td><strong>max(str)<\/strong>Returns the max alphabetical character from the string str.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">25<\/td>\n<td><strong>min(str)<\/strong>Returns the min alphabetical character from the string str.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">26<\/td>\n<td><strong>replace(old, new [, max])<\/strong>Replaces all occurrences of old in string with new or at most max occurrences if max given.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">27<\/td>\n<td><strong>rfind(str, beg=0,end=len(string))<\/strong>Same as find(), but search backwards in string.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">28<\/td>\n<td><strong>rindex( str, beg=0, end=len(string))<\/strong>Same as index(), but searched backwards in the given string.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">29<\/td>\n<td><strong>rjust(width,[, fillchar])<\/strong>Returns a space-padded string with the original string right-justified to a total of width columns.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">30<\/td>\n<td><strong>rstrip()<\/strong>Removes all trailing whitespace of string.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">31<\/td>\n<td><strong>split(str=&#8221;&#8221;, num=string.count(str))<\/strong>Splits the string according to delimiter str (space if not provided) and returns list of substrings; split into at most num substrings if specified.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">32<\/td>\n<td><strong>splitlines( num=string.count(&#8216;\\n&#8217;))<\/strong>Splits string at all (or num) NEWLINEs and returns a list of each line with NEWLINEs removed.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">33<\/td>\n<td><strong>startswith(str, beg=0,end=len(string))<\/strong>Checks if string or a substring of string (if starting index beg and ending index end are given) starts with substring str. Returns true if yes. Returns false if otherwise<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">34<\/td>\n<td><strong>strip([chars])<\/strong>Performs both lstrip() and rstrip() on string.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">35<\/td>\n<td><strong>swapcase()<\/strong>Coverts uppercase letters to lowercase and lowercase letters to uppercase in a string.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">36<\/td>\n<td><strong>title<\/strong>()Returns the\u00a0 &#8220;titlecased&#8221; version of string. This means that all words begin with uppercase and the rest are lowercase.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">37<\/td>\n<td><strong>translate(table, deletechars=&#8221;&#8221;)<\/strong>Translates string according to translation table str(256 chars), removing those in the del string.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">38<\/td>\n<td><strong>upper()<\/strong>Converts the lowercase letters in string to uppercase.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">39<\/td>\n<td><strong>zfill (width)<\/strong>Returns the original string leftpadded with 0s to a total of width characters; intended for numbers. zfill() retains any sign given .<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">40<\/td>\n<td><strong>isdecimal()<\/strong>Returns true if a unicode string contains only decimal characters. Returns false if othewise.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t9\">9. Watch the video<\/strong><br \/>\n<iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/4s7q9AuYU0c\" width=\"100%\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>We would cover strings under the following topics: Introduction Accessing String Values Escape Characters Special String Operators Formatting a String Multi-line Strings Unicode Characters String &hellip; <\/p>\n","protected":false},"author":395,"featured_media":162,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[7],"class_list":["post-103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-python-strings"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python - Strings - Python Tutorials Python Tutorials<\/title>\n<meta name=\"description\" content=\"In this lesson we cover strings in Python. We examine how to create and modify strings as well as how to manipulate string using string methods\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python - Strings - Python Tutorials Python Tutorials\" \/>\n<meta property=\"og:description\" content=\"In this lesson we cover strings in Python. We examine how to create and modify strings as well as how to manipulate string using string methods\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-01T01:43:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-02T03:53:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Strings-in-Python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"922\" \/>\n\t<meta property=\"og:image:height\" content=\"509\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/09-python-strings\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/09-python-strings\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Python &#8211; Strings\",\"datePublished\":\"2019-02-01T01:43:02+00:00\",\"dateModified\":\"2019-03-02T03:53:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/09-python-strings\\\/\"},\"wordCount\":1647,\"commentCount\":3,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/09-python-strings\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Strings-in-Python.jpg\",\"keywords\":[\"Python Strings\"],\"articleSection\":[\"Python Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/09-python-strings\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/09-python-strings\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/09-python-strings\\\/\",\"name\":\"Python - Strings - Python Tutorials Python Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/09-python-strings\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/09-python-strings\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Strings-in-Python.jpg\",\"datePublished\":\"2019-02-01T01:43:02+00:00\",\"dateModified\":\"2019-03-02T03:53:03+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"description\":\"In this lesson we cover strings in Python. We examine how to create and modify strings as well as how to manipulate string using string methods\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/09-python-strings\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/09-python-strings\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/09-python-strings\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Strings-in-Python.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Strings-in-Python.jpg\",\"width\":922,\"height\":509,\"caption\":\"Working with strings in Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/09-python-strings\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python &#8211; Strings\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/\",\"name\":\"Python Tutorials\",\"description\":\"Python Tutorial for Programming and Data Science\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python - Strings - Python Tutorials Python Tutorials","description":"In this lesson we cover strings in Python. We examine how to create and modify strings as well as how to manipulate string using string methods","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/","og_locale":"en_US","og_type":"article","og_title":"Python - Strings - Python Tutorials Python Tutorials","og_description":"In this lesson we cover strings in Python. We examine how to create and modify strings as well as how to manipulate string using string methods","og_url":"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/","og_site_name":"Python Tutorials","article_published_time":"2019-02-01T01:43:02+00:00","article_modified_time":"2019-03-02T03:53:03+00:00","og_image":[{"width":922,"height":509,"url":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Strings-in-Python.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Python &#8211; Strings","datePublished":"2019-02-01T01:43:02+00:00","dateModified":"2019-03-02T03:53:03+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/"},"wordCount":1647,"commentCount":3,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Strings-in-Python.jpg","keywords":["Python Strings"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/","url":"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/","name":"Python - Strings - Python Tutorials Python Tutorials","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Strings-in-Python.jpg","datePublished":"2019-02-01T01:43:02+00:00","dateModified":"2019-03-02T03:53:03+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"description":"In this lesson we cover strings in Python. We examine how to create and modify strings as well as how to manipulate string using string methods","breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Strings-in-Python.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Strings-in-Python.jpg","width":922,"height":509,"caption":"Working with strings in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/python\/09-python-strings\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/python\/"},{"@type":"ListItem","position":2,"name":"Python &#8211; Strings"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/python\/#website","url":"https:\/\/www.kindsonthegenius.com\/python\/","name":"Python Tutorials","description":"Python Tutorial for Programming and Data Science","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/python\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/comments?post=103"}],"version-history":[{"count":4,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/103\/revisions"}],"predecessor-version":[{"id":165,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/103\/revisions\/165"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media\/162"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media?parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/categories?post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/tags?post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}