String Methods in Python | Python Tutorial - Day #13

String Methods in Python | Python Tutorial - Day #13

100 Days Of Code: String Methods

Introduction to String Methods

  • The video series is likened to a Netflix show, generating excitement among viewers. Today's focus is on 'String' methods in Python.
  • The presenter will demonstrate how to perform operations on strings, such as converting them to uppercase or lowercase and finding their length.

Understanding Immutability of Strings

  • A string example a = "harry" is introduced, with its length being 5. The presenter emphasizes the immutability of strings in Python.
  • Strings cannot be changed in place; instead, methods create new strings. For instance, using print(a.upper()) generates a new string "HARRY" without altering the original.

Key String Methods Explained

  • The concept of immutability is reiterated for clarity: "Strings are immutable."
  • The lower() method converts a string to lowercase but also creates a new string rather than modifying the existing one.

Additional String Operations

  • The rstrip() method removes trailing characters from a string. For example, applying it to "Harry!!!!!!!" would strip the exclamation marks at the end but not at the beginning.
  • The replace() method substitutes all occurrences of a specified substring with another substring. For instance, replacing "harry" with "John" changes every instance within the string.

Splitting Strings into Lists

  • The split() method divides a string into a list based on specified delimiters (e.g., spaces). This can create multiple elements from one string.
  • An example illustrates how splitting works when there are spaces present in the original string.

Capitalization and Practical Applications

  • Although not covered extensively here, capitalizing strings can be useful for formatting text like blog headings.
  • A practical scenario is presented where capitalization helps correct hurriedly typed headings that may lack proper formatting.

Understanding String Methods in Python

Capitalizing Strings

  • The capitalize() method converts the first character of a string to uppercase, enhancing its appearance. For example, "Introduction to js" becomes "Introduction to js".
  • Common human errors like typing "tO" or "jS" are automatically corrected by capitalize(), which changes all subsequent characters to lowercase.
  • The method's description states that it only affects the first character and has no effect if it's already uppercase.

Centering Strings

  • The center() method aligns a string in the center based on a specified width. For instance, using str1.center(50) adds spaces before the string to make its total length 50.
  • Demonstrating with lengths: len(str1) is 25, while len(str1.center(50)) is 50, indicating that 25 spaces were added for centering.

Counting Characters

  • The count() method counts occurrences of a specific substring within a string. For example, calling a.count("Harry") returns how many times "Harry" appears in the string.

Checking String Endings

  • The endswith() function checks if a string ends with specified characters. It returns a Boolean value (True/False), useful for conditional statements.
  • Users can check for substrings within specific index ranges by providing start and end positions in the syntax: str1.endswith(substring, start_index, end_index).

Finding Substrings

  • The find() method searches for the first occurrence of a substring and returns its index; if not found, it returns -1.
  • An example illustrates that searching for "is" in "He's name is Dan" will return index 10 as it finds the first occurrence of "is".

Index Method Comparison

  • Similar to find(), the index() method also locates substrings but raises an error instead of returning -1 when not found. This distinction is crucial for error handling in code execution.

Understanding String Methods in Python

Using the index Method

  • The index method raises an exception if the substring is not found, which can be useful for ensuring that a program exits upon encountering an error.
  • An example of the raised exception is "ValueError: substring not found." This indicates that the specified substring does not exist within the string.

Exploring isalnum() and isalpha()

  • The isalnum() method checks if a string consists solely of alphanumeric characters (A-Z, a-z, 0-9) and returns True only if this condition is met. Otherwise, it returns False.
  • If any non-alphanumeric characters are present, such as punctuation or spaces, isalnum() will return False. For instance, "Welcome to the console" would yield an error due to non-alphanumeric content.
  • In contrast, the isalpha() method checks for alphabetic characters only (A-Z or a-z). It returns True if all characters are letters; otherwise, it returns False. For example, "Welcome00" would return 'False' because of numeric digits.

Additional String Methods: Lowercase and Printable Checks

  • The method islower() verifies whether all characters in a string are lowercase; it returns 'True' if they are and 'False' otherwise. This can be tested using various strings in Replit's interface.
  • The method isprintable() checks if all characters in a string are printable. It will return 'True' unless there are unprintable characters like newline (n). Adding such characters results in 'False'.

Understanding Whitespace with isspace()

  • The method isspace() determines whether a string contains only whitespace characters (spaces or tabs). It will return 'True' when whitespace is present and 'False' otherwise. For example, adding spaces via the space bar yields 'True'.

Title Case Check with istitle()

  • The method istitle() checks if each word in a string starts with an uppercase letter; it returns 'True' only under this condition. If any word does not start with an uppercase letter, it will return 'False'. This was demonstrated using titles from blogs.

Other Useful String Methods

  • The methods like startswith() and its counterpart can check whether strings begin or end with specific substrings respectively; they both return 'True' when matched correctly. For instance, checking for "True" at the beginning of a string yields true output when applicable.
  • The function swapcase() switches uppercase letters to lowercase and vice versa within a given string—this straightforward operation allows easy manipulation of text cases for various applications like formatting titles or names effectively.

Conclusion on Recording Setup

  • Towards the end of the discussion, insights into recording setups were shared including preferences for MacBook over Windows due to personal comfort while working on projects like video creation using OBS software alongside iPhone's Continuity Camera feature for enhanced recording quality were mentioned briefly but noted as separate topics for future discussions.

Using iPhone as a Webcam for MacBook

Introduction to iPhone as a Webcam

  • The speaker discusses the capability of using an iPhone as a webcam when brought close to a MacBook with Bluetooth and Wi-Fi enabled.
  • The quality of the video is noted to be impressive, showcasing the potential of utilizing an iPhone for this purpose.

Video Quality and Effects

  • While acknowledging that the quality can be improved, the speaker expresses satisfaction with the current performance of the iPhone's camera.
  • The speaker demonstrates various video effects available, including 'Center Image' and 'Portrait', highlighting how these options alter image presentation.

Exploring Video Effects

  • Three main video effects are introduced: 'Studio Light', which adjusts lighting; 'Portrait', which blurs background elements; and their impact on microphone visibility is discussed.
  • The speaker notes that while some effects may not work perfectly (e.g., blurring unintended objects), they anticipate improvements from Apple in future updates.

Desk View Feature

  • A feature called 'Desk View' is showcased, allowing viewers to see the speaker's desk setup clearly, emphasizing its utility for presentations or demonstrations.

Conclusion on Equipment Use

  • The speaker concludes by mentioning their use of an upgraded microphone alongside their iPhone setup, indicating that good quality equipment enhances video enjoyment.
Video description

Python is one of the most demanded programming languages in the job market. Surprisingly, it is equally easy to learn and master Python. This python tutorial for absolute beginners in Hindi series will focus on teaching you python concepts from the ground up. Access the Playlist: https://www.youtube.com/playlist?list=PLu0W_9lII9agwh1XjRt242xIpHhPT2llg Link to the Repl: https://replit.com/@codewithharry/13-Day13-String-Methods#main.py Join Replit the browser-based IDE used in this course - https://join.replit.com/code-with-harry-100-doc ►Checkout my English channel here: https://www.youtube.com/channel/UC7btqG2Ww0_2LwuQxpvo2HQ ►Instagram: www.instagram.com/codewithharry python, C, C++, Java, JavaScript and Other Cheetsheets [++]: Playlist: https://www.youtube.com/playlist?list=PLu0W_9lII9agrsRZjFECeFuWY5ev2pQlk ►Learn in One Video[++]: Python[15 Hr]: https://www.youtube.com/watch?v=gfDE2a7MKjA&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Python Advance[3.5 Hr]: https://www.youtube.com/watch?v=61a7UkDO50s&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Python[1 Hr]: https://www.youtube.com/watch?v=qHJjMvHLJdg&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Python[2 Hr]: https://www.youtube.com/watch?v=ihk_Xglr164&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Python[15 Min]:https://www.youtube.com/watch?v=fr1f84rg4Nw&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 JavaScript[1 Hr]: https://www.youtube.com/watch?v=onbBV0uFVpo&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 C[1.3 Hr]-https://www.youtube.com/watch?v=YXcgD8hRHYY&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 php[1 Hr]: https://www.youtube.com/watch?v=xW7ro3lwaCI&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 php[2.3 Hr]:https://www.youtube.com/watch?v=1SnPKhCdlsU&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 php[Project]- https://www.youtube.com/watch?v=-al2bECumKg&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 HTML[30 Min]:https://www.youtube.com/watch?v=E3ByCRqE7Lo&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 CSS[8.5 Hr]:https://www.youtube.com/watch?v=Edsxf_NBFrw&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 CSS[1.4 Hr]:https://www.youtube.com/watch?v=u5-K_ua9sOw&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Wordpress[3.2 Hr]:https://www.youtube.com/watch?v=GlLRYml8mCY&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Angular[2 Hr]:https://www.youtube.com/watch?v=0LhBvp8qpro&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Java[2.3 Hr]:https://www.youtube.com/watch?v=rV_3Lewxx6o&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Web Scraping[1 Hr]:https://www.youtube.com/watch?v=uufDGjTuq34&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 MongoDB[2 Hr]:https://www.youtube.com/watch?v=oSIv-E60NiU&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Numpy[1 Hr]:https://www.youtube.com/watch?v=Rbh1rieb3zc&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Android Dev[12 Hr]- https://www.youtube.com/watch?v=mXjZQX3UzOs Linux[1 Hr]:https://www.youtube.com/watch?v=_tCY-c-sPZc&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 JQuery[1.1 Hr]:https://www.youtube.com/watch?v=YFlx1C8XwR0&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Git and GitHub[1.1 Hr]:https://www.youtube.com/watch?v=gwWKnnCMQ5c&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 ►Complete course [playlist]: React: https://www.youtube.com/playlist?list=PLu0W_9lII9agx66oZnT6IyhcMIbUMNMdt Python-https://www.youtube.com/playlist?list=PLu0W_9lII9agICnT8t4iYVSZ3eykIAOME OOP Python-https://www.youtube.com/playlist?list=PLu0W_9lII9ahfRrhFcoB-4lpp9YaBmdCP Java:https://www.youtube.com/playlist?list=PLu0W_9lII9agS67Uits0UnJyrYiXhDS6q JavaScript- https://www.youtube.com/playlist?list=PLu0W_9lII9ajyk081To1Cbt2eI5913SsL PHP-https://www.youtube.com/playlist?list=PLu0W_9lII9aikXkRE0WxDt1vozo3hnmtR C-https://www.youtube.com/playlist?list=PLu0W_9lII9aiXlHcLx-mDH1Qul38wD3aR C++-https://www.youtube.com/playlist?list=PLu0W_9lII9agpFUAlPFe_VNSlXW5uE0YL Git & GitHub-https://www.youtube.com/playlist?list=PLu0W_9lII9ahVQekD7ePHmnirTePXwIln Android Dev- https://www.youtube.com/playlist?list=PLu0W_9lII9aiL0kysYlfSOUgY5rNlOhUd Python GUI- https://www.youtube.com/playlist?list=PLu0W_9lII9ajLcqRcj4PoEihkukF_OTzA Web Development- https://www.youtube.com/playlist?list=PLu0W_9lII9agiCUZYRsvtGTXdxkzPyItg Python Django:https://www.youtube.com/playlist?list=PLu0W_9lII9ah7DDtYtflgwMwpT3xmjXY9 Projects Using HTML, CSS & Javascript- https://www.youtube.com/playlist?list=PLu0W_9lII9aiQiOwthuSvinxoflmhRxM3 Data Structure and Algo:https://www.youtube.com/playlist?list=PLu0W_9lII9ahIappRPN0MCAgtOu3lQjQi Follow Me On Social Media ►Website (created using Django Rest & Angular): https://www.codewithharry.com ►Facebook: https://www.facebook.com/CodeWithHarry ►Instagram: https://www.instagram.com/codewithharry/ Twitter: https://twitter.com/CodeWithHarry Comment "#HarryBhai" if you read this 😉😉