{"id":60,"date":"2025-10-21T04:20:14","date_gmt":"2025-10-21T04:20:14","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=60"},"modified":"2025-12-17T07:50:26","modified_gmt":"2025-12-17T07:50:26","slug":"lesson-8-strings-and-string-methods","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=60","title":{"rendered":"Lesson 8: Strings and String Methods"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To understand how to work with text data in Python, perform string operations, and use built-in string methods for manipulation and analysis.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde0 <strong>1. What Is a String?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>string<\/strong> is a sequence of characters enclosed in <strong>single quotes (&#8221;)<\/strong>, <strong>double quotes (&#8220;&#8221;)<\/strong>, or <strong>triple quotes (&#8221;&#8217; &#8221;&#8217;)<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Sameer\"\ngreeting = 'Hello!'\nmessage = '''Welcome to Python programming.'''\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2699\ufe0f <strong>2. Accessing String Characters<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Strings are <strong>indexed<\/strong>, meaning each character has a position (starting from 0).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Python\"\nprint(text&#91;0])   # First character\nprint(text&#91;5])   # Last character\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>P\nn\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd04 <strong>3. String Slicing<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can extract parts of a string using slicing.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>word = \"Programming\"\nprint(word&#91;0:6])     # Output: Progra\nprint(word&#91;3:])      # Output: gramming\nprint(word&#91;:5])      # Output: Progr\nprint(word&#91;-3:])     # Output: ing\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde9 <strong>4. String Concatenation and Repetition<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can <strong>join strings<\/strong> using <code>+<\/code> and <strong>repeat<\/strong> using <code>*<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = \"Hello\"\nb = \"Python\"\nprint(a + \" \" + b)   # Concatenation\nprint(a * 3)         # Repetition\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello Python\nHelloHelloHello\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd22 <strong>5. String Length<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Find the number of characters using <code>len()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Python\"\nprint(len(text))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>6\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2702\ufe0f <strong>6. Checking for Substrings<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can check if a substring exists using the <code>in<\/code> or <code>not in<\/code> operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Learning Python\"\nprint(\"Python\" in text)\nprint(\"Java\" not in text)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True\nTrue\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\uddf0 <strong>7. Common String Methods<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Method<\/th><th>Description<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td><code>upper()<\/code><\/td><td>Converts all characters to uppercase<\/td><td><code>\"hello\".upper()<\/code> \u2192 <code>HELLO<\/code><\/td><\/tr><tr><td><code>lower()<\/code><\/td><td>Converts all characters to lowercase<\/td><td><code>\"HELLO\".lower()<\/code> \u2192 <code>hello<\/code><\/td><\/tr><tr><td><code>title()<\/code><\/td><td>Capitalizes first letter of each word<\/td><td><code>\"hello world\".title()<\/code> \u2192 <code>Hello World<\/code><\/td><\/tr><tr><td><code>strip()<\/code><\/td><td>Removes spaces from start and end<\/td><td><code>\" hello \".strip()<\/code> \u2192 <code>hello<\/code><\/td><\/tr><tr><td><code>replace()<\/code><\/td><td>Replaces substring<\/td><td><code>\"I love Java\".replace(\"Java\", \"Python\")<\/code> \u2192 <code>I love Python<\/code><\/td><\/tr><tr><td><code>split()<\/code><\/td><td>Splits string into list<\/td><td><code>\"a,b,c\".split(\",\")<\/code> \u2192 <code>['a', 'b', 'c']<\/code><\/td><\/tr><tr><td><code>join()<\/code><\/td><td>Joins list into a string<\/td><td><code>\"-\".join(['a','b','c'])<\/code> \u2192 <code>a-b-c<\/code><\/td><\/tr><tr><td><code>find()<\/code><\/td><td>Returns index of substring<\/td><td><code>\"python\".find(\"th\")<\/code> \u2192 <code>2<\/code><\/td><\/tr><tr><td><code>count()<\/code><\/td><td>Counts occurrences<\/td><td><code>\"apple\".count(\"p\")<\/code> \u2192 <code>2<\/code><\/td><\/tr><tr><td><code>startswith()<\/code> \/ <code>endswith()<\/code><\/td><td>Checks prefix\/suffix<\/td><td><code>\"Python\".startswith(\"Py\")<\/code> \u2192 <code>True<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd24 <strong>8. Escape Characters<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Used to include special characters inside strings.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Escape Code<\/th><th>Meaning<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td><code>\\'<\/code><\/td><td>Single quote<\/td><td><code>'It\\'s fun'<\/code><\/td><\/tr><tr><td><code>\\\"<\/code><\/td><td>Double quote<\/td><td><code>\"He said \\\"Hi\\\"\"<\/code><\/td><\/tr><tr><td><code>\\\\<\/code><\/td><td>Backslash<\/td><td><code>\"C:\\\\Users\"<\/code><\/td><\/tr><tr><td><code>\\n<\/code><\/td><td>New line<\/td><td><code>\"Hello\\nWorld\"<\/code><\/td><\/tr><tr><td><code>\\t<\/code><\/td><td>Tab space<\/td><td><code>\"Hello\\tWorld\"<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Name:\\tSameer\\nCity:\\tWarangal\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Name:   Sameer\nCity:   Warangal\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\uddee <strong>9. String Formatting<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can use <strong>f-strings<\/strong> or <code>format()<\/code> for neat output.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"Sameer\"\nage = 25\nprint(f\"My name is {name} and I am {age} years old.\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>My name is Sameer and I am 25 years old.\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udf0d <strong>10. Real-Life Example: Text Analyzer<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>text = input(\"Enter a sentence: \")\nprint(\"Total characters:\", len(text))\nprint(\"Uppercase:\", text.upper())\nprint(\"Number of spaces:\", text.count(\" \"))\nprint(\"Reversed:\", text&#91;::-1])\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output (for input \u201cPython is fun\u201d):<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Total characters: 13\nUppercase: PYTHON IS FUN\nNumber of spaces: 2\nReversed: nuf si nohtyP\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To understand how to work with text data in Python, perform string operations, and use built-in string methods for manipulation and analysis. \ud83e\udde0 1. What Is a String? A string is a sequence of characters enclosed in single quotes (&#8221;), double quotes (&#8220;&#8221;), or triple quotes (&#8221;&#8217; &#8221;&#8217;). \u2705 Example: \u2699\ufe0f 2. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-60","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=60"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":61,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/60\/revisions\/61"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}