Mastering Python : Day 5

Strings in Python

In Python, a string is a sequence of characters enclosed in quotes (single, double, or triple quotes). Strings are immutable, meaning they cannot be changed after creation.

String Methods in Python

Python provides a wide range of methods for manipulating and working with strings. Here are some of the most commonly used string methods:

1. len()

Returns the length of the string.

code1 my_string = "Hello, World!"
print(len(my_string))  # Output: 13

2. upper() and lower()

Converts the string to uppercase or lowercase.

code1my_string = "Hello, World!"
2print(my_string.upper())  # Output: "HELLO, WORLD!"
3print(my_string.lower())  # Output: "hello, world!"

3. strip()

Removes leading and trailing whitespace from the string.

 code1my_string = "   Hello, World!   "
2print(my_string.strip())  # Output: "Hello, World!"

4. split()

Splits the string into a list of substrings based on a specified separator.

 code1my_string = "apple,banana,cherry"
2print(my_string.split(","))  # Output: ["apple", "banana", "cherry"]

5. join()

Joins a list of strings into a single string.

code1my_list = ["apple", "banana", "cherry"]
2print(",".join(my_list))  # Output: "apple,banana,cherry"

6. find() and index()

Returns the index of the first occurrence of a specified substring.

code1my_string = "Hello, World!"
2print(my_string.find("World"))  # Output: 7
3print(my_string.index("World"))  # Output: 7

7. replace()

Replaces occurrences of a specified substring with another substring.

 code1my_string = "Hello, World!"
2print(my_string.replace("World", "Universe"))  # Output: "Hello, Universe!"

Practice Questions on Strings in Python

Here are some practice questions to help you reinforce your understanding of strings in Python:

1. String Concatenation

Write a Python program to concatenate two strings "Hello, " and "World!" using the + operator.

code1# Your code here

2. String Repetition

Write a Python program to repeat the string "Hello, World!" three times using the * operator.

pythonVerifyOpen In EditorEditCopy code1# Your code here

3. String Indexing

Write a Python program to extract the first character of the string "Hello, World!" using indexing.

 code1# Your code here

4. String Slicing

Write a Python program to extract the substring "World" from the string "Hello, World!" using slicing.

 code1# Your code here

5. String Formatting

Write a Python program to format the string "My name is {} and I am {} years old." with the values "John" and 30 using the format() method.

code1# Your code here

6. String Splitting

Write a Python program to split the string "apple,banana,cherry" into a list of substrings using the comma as a separator.

 code1# Your code here

7. String Joining

Write a Python program to join the list of strings ["apple", "banana", "cherry"] into a single string using the comma as a separator.

 code1# Your code here

8. String Searching

Write a Python program to find the index of the first occurrence of the substring "World" in the string "Hello, World!".

 code1# Your code here

9. String Replacing

Write a Python program to replace all occurrences of the substring "World" with "Universe" in the string "Hello, World!".

 code1# Your code here

10. String Validation

Write a Python program to check if the string "Hello, World!" contains only alphanumeric characters.

 code1# Your code here
String Methods:
MethodDescription
capitalize()Converts the first character to upper case
casefold()Converts string into lower case
center()Returns a centered string
count()Returns the number of times a specified value occurs in a string
encode()Returns an encoded version of the string
endswith()Returns true if the string ends with the specified value
expandtabs()Sets the tab size of the string
find()Searches the string for a specified value and returns the position of where it was found
format()Formats specified values in a string
format_map()Formats specified values in a string
index()Searches the string for a specified value and returns the position of where it was found
isalnum()Returns True if all characters in the string are alphanumeric
isalpha()Returns True if all characters in the string are in the alphabet
isascii()Returns True if all characters in the string are ascii characters
isdecimal()Returns True if all characters in the string are decimals
isdigit()Returns True if all characters in the string are digits
isidentifier()Returns True if the string is an identifier
islower()Returns True if all characters in the string are lower case
isnumeric()Returns True if all characters in the string are numeric
isprintable()Returns True if all characters in the string are printable
isspace()Returns True if all characters in the string are whitespaces
istitle()Returns True if the string follows the rules of a title
isupper()Returns True if all characters in the string are upper case
join()Joins the elements of an iterable to the end of the string
ljust()Returns a left justified version of the string
lower()Converts a string into lower case
lstrip()Returns a left trim version of the string
maketrans()Returns a translation table to be used in translations
partition()Returns a tuple where the string is parted into three parts
replace()Returns a string where a specified value is replaced with a specified value
rfind()Searches the string for a specified value and returns the last position of where it was found
rindex()Searches the string for a specified value and returns the last position of where it was found
rjust()Returns a right justified version of the string
rpartition()Returns a tuple where the string is parted into three parts
rsplit()Splits the string at the specified separator, and returns a list
rstrip()Returns a right trim version of the string
split()Splits the string at the specified separator, and returns a list
splitlines()Splits the string at line breaks and returns a list
startswith()Returns true if the string starts with the specified value
strip()Returns a trimmed version of the string
swapcase()Swaps cases, lower case becomes upper case and vice versa
title()Converts the first character of each word to upper case
translate()Returns a translated string
upper()Converts a string into upper case
zfill()Fills the string with a specified number of 0 values at the beginning