String Operations Part(2) In Python

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 72:-  String Operations Part(2) In Python

Continuing from the previous string operations, here are some more commonly used string operations in Python:

  1. Removing Whitespace: You can remove leading and trailing whitespace from a string using the strip(), lstrip(), and rstrip() methods.

pythonCopy code

string = "   Hello, World!   " print(string.strip())    # Output: "Hello, World!" print(string.lstrip())   # Output: "Hello, World!   " print(string.rstrip())   # Output: "   Hello, World!"

  1. String Splitting and Joining: The split() method splits a string into a list of substrings based on a delimiter. The join() method joins a list of strings into a single string using a specified delimiter.

pythonCopy code

string = "apple,banana,orange" fruits_list = string.split(",") print(fruits_list)   # Output: ['apple', 'banana', 'orange'] fruits = ["apple", "banana", "orange"] fruits_string = ",".join(fruits) print(fruits_string)   # Output: "apple,banana,orange"

  1. Finding Substrings and Counting Occurrences: The find() method finds the index of the first occurrence of a substring. If the substring is not found, it returns -1. The count() method counts the number of occurrences of a substring in a string.

pythonCopy code

string = "Hello, Hello, World!" print(string.find("Hello"))   # Output: 0 print(string.find("Python"))  # Output: -1 print(string.count("Hello"))  # Output: 2

  1. String Formatting with str.format(): The str.format() method allows you to format strings with placeholders and replacement fields.

pythonCopy code

name = "Alice" age = 30 formatted_string = "My name is {} and I am {} years old.".format(name, age) print(formatted_string)   # Output: "My name is Alice and I am 30 years old."

  1. String Reversal: You can reverse a string using slicing.

pythonCopy code

string = "Hello, World!" reversed_string = string[::-1] print(reversed_string)   # Output: "!dlroW ,olleH"

  1. Checking String Types: Python provides several methods to check if a string has a specific type of characters.

pythonCopy code

text = "Hello123" print(text.isalpha())   # Output: False (Contains digits) print(text.isdigit())   # Output: False (Contains alphabetic characters) print(text.isalnum())   # Output: True (Contains only alphabetic characters and digits) print(text.islower())   # Output: False (Not all characters are lowercase) print(text.isupper())   # Output: False (Not all characters are uppercase)

These are some additional string operations that can be very useful when working with strings in Python. Python's built-in string methods provide a wide range of functionalities, making string processing efficient and straightforward.

8. String

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?