{"id":51,"date":"2022-08-29T07:42:30","date_gmt":"2022-08-29T07:42:30","guid":{"rendered":"https:\/\/python.freelinuxtutorials.com\/?p=51"},"modified":"2022-08-29T07:44:46","modified_gmt":"2022-08-29T07:44:46","slug":"pythonstring-methods","status":"publish","type":"post","link":"https:\/\/python.freelinuxtutorials.com\/index.php\/2022\/08\/29\/pythonstring-methods\/","title":{"rendered":"Python>>String Methods"},"content":{"rendered":"<p>Here are some common String Methods or Attributes that you can use in modifying strings.<\/p>\n<p>Assuming x is the string:<\/p>\n<p>a. x.upper()\u00a0 &#8211; converts to upper case<\/p>\n<blockquote><p>&gt; x = &#8216;Free Linux Tutorials&#8217;<br \/>\n&gt;&gt;&gt; x.upper()<br \/>\n&#8216;FREE LINUX TUTORIALS&#8217;<br \/>\n&gt;&gt;&gt; print(x)<br \/>\nFree Linux Tutorials<br \/>\n&gt;&gt;&gt; y = x.upper()<br \/>\n&gt;&gt;&gt; print (y)<br \/>\nFREE LINUX TUTORIALS<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>b. \u00a0x.lower() = converts to lower case<\/p>\n<blockquote><p>&gt; print(x)<br \/>\nFree Linux Tutorials<br \/>\n&gt;&gt;&gt; x.lower()<br \/>\n&#8216;free linux tutorials&#8217;<br \/>\n&gt;&gt;&gt; y = x.lower()<br \/>\n&gt;&gt;&gt; print(y)<br \/>\nfree linux tutorials<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>c. x.split() = to split strings<\/p>\n<blockquote><p>&gt; print(x)<br \/>\nFree Linux Tutorials<br \/>\n&gt;&gt;&gt; x.split()<br \/>\n[&#8216;Free&#8217;, &#8216;Linux&#8217;, &#8216;Tutorials&#8217;]<\/p>\n<p>&gt; x.split(&#8216;r&#8217;)<br \/>\n[&#8216;F&#8217;, &#8216;ee Linux Tuto&#8217;, &#8216;ials&#8217;]<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>d. \u00a0len(x) = to get the length of strings or number of characters<\/p>\n<blockquote><p>&gt; print(x)<br \/>\nFree Linux Tutorials<br \/>\n&gt;&gt;&gt; len(x)<br \/>\n20<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>e. x.count() = count the number of times that the character\/words appear in the string<\/p>\n<blockquote><p>x = &#8216;This These That Those&#8217;<br \/>\n&gt;&gt;&gt; x.count(&#8216;t&#8217;)<br \/>\n1<br \/>\n&gt;&gt;&gt; x.count(&#8216;T&#8217;)<br \/>\n4<\/p>\n<p>&gt;&gt; x = &#8216;I love apples. We love apples. They love apples.&#8217;<br \/>\n&gt;&gt;&gt; x.count(&#8216;apples&#8217;)<br \/>\n3<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>f. x.find() = to find character(s) in the string<\/p>\n<blockquote><p>&gt;&gt; print (x)<br \/>\nI love apples. We love apples. They love apples.<br \/>\n&gt;&gt;&gt; x.find(&#8216;apples&#8217;)<br \/>\n7<\/p><\/blockquote>\n<p>where 7 is the 7th position (from previous lesson, it always start with 0)<\/p>\n<blockquote><p>&gt;&gt; x.find(&#8216;We&#8217;)<br \/>\n15<br \/>\n&gt;&gt;&gt; x.find(&#8216;They love&#8217;)<br \/>\n31<\/p>\n<p>&nbsp;<\/p>\n<p>x.find(&#8216;apples&#8217;,7)<br \/>\n7<\/p><\/blockquote>\n<p>begin searching on 7th position (1st word apple is on 7th position)<\/p>\n<blockquote><p>&gt;&gt; x.find(&#8216;apples&#8217;,8)<br \/>\n23<\/p><\/blockquote>\n<p>begin searching on 8th position (1st word apple is excluded since it is on 7th position, so it starts counting on 8th)<\/p>\n<p>&nbsp;<\/p>\n<p>g.\u00a0 .replace() = to replace characters(s)<\/p>\n<blockquote><p>&gt; print(x)<br \/>\nI love apples. We love apples. They love apples.<br \/>\n&gt;&gt;&gt; x.replace(&#8216;apples&#8217;,&#8217;oranges&#8217;)<br \/>\n&#8216;I love oranges. We love oranges. They love oranges.&#8217;<\/p>\n<p>&gt;&gt; x.replace(&#8216;I&#8217;,&#8217;We&#8217;)<br \/>\n&#8216;We love apples. We love apples. They love apples.&#8217;<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Here are some common String Methods or Attributes that you can use in modifying strings. Assuming x is the string: a. x.upper()\u00a0 &#8211; converts to upper case &gt; x = &#8216;Free Linux Tutorials&#8217; &gt;&gt;&gt; x.upper() &#8216;FREE LINUX TUTORIALS&#8217; &gt;&gt;&gt; print(x) Free Linux Tutorials &gt;&gt;&gt; y = x.upper() &gt;&gt;&gt; print (y) FREE LINUX TUTORIALS &nbsp; b&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"footnotes":""},"categories":[3],"tags":[12,18,16,17,14,11,15,9,13],"class_list":["post-51","post","type-post","status-publish","format-standard","hentry","category-data-types","tag-attributes","tag-count","tag-find","tag-len","tag-lower","tag-methods","tag-replace","tag-string","tag-upper"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":3,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":54,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/51\/revisions\/54"}],"wp:attachment":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/categories?post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/tags?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}