23 Jan Python Prog 1 (Vrushali)
Imports and Function Definition
def main():- Defines the main function, where the program logic resides.
1. Basic String Declaration
sample_text = "Hello, World!"- Declares a string variable
sample_textwith the value"Hello, World!".
- Declares a string variable
print(f"Original Text: {sample_text}")- Prints the original text using an f-string for formatting.
2. String Length
print(f"Length of string: {len(sample_text)}")- Uses the
len()function to find and print the length ofsample_text.
- Uses the
3. String Slicing
print(f"First 5 characters: {sample_text[:5]}")- Slices the first 5 characters using
[:5]and prints them.
- Slices the first 5 characters using
print(f"Last 5 characters: {sample_text[-5:]}")- Slices the last 5 characters using
[-5:]and prints them.
- Slices the last 5 characters using
4. Changing Case
print(f"Uppercase: {sample_text.upper()}")- Converts the string to uppercase using
.upper()and prints it.
- Converts the string to uppercase using
print(f"Lowercase: {sample_text.lower()}")- Converts the string to lowercase using
.lower()and prints it.
- Converts the string to lowercase using
print(f"Title Case: {sample_text.title()}")- Converts the string to title case using
.title()and prints it.
- Converts the string to title case using
print(f"Swap Case: {sample_text.swapcase()}")- Swaps the case of each character using
.swapcase()and prints it.
- Swaps the case of each character using
5. Searching Within a String
substring = "World"- Assigns
"World"to the variablesubstring.
- Assigns
print(f"Does the string contain '{substring}'? {'Yes' if substring in sample_text else 'No'}")- Checks if
substringis insample_textand prints the result.
- Checks if
print(f"Index of '{substring}': {sample_text.find(substring)}")- Finds the starting index of
substringusing.find()and prints it.
- Finds the starting index of
6. Replacing Text
replaced_text = sample_text.replace("World", "Python")- Replaces
"World"with"Python"insample_textand assigns it toreplaced_text.
- Replaces
print(f"Replaced Text: {replaced_text}")- Prints the modified string.
7. Splitting and Joining Strings
words = sample_text.split(", ")- Splits
sample_textat", "and stores the resulting list inwords.
- Splits
print(f"Split into words: {words}")- Prints the list of words.
joined_text = " - ".join(words)- Joins the elements of
wordswith" - "as a separator.
- Joins the elements of
print(f"Joined with ' - ': {joined_text}")- Prints the joined string.
8. Trimming Whitespace
padded_text = " Hello, Python! "- Assigns a string with leading and trailing spaces to
padded_text.
- Assigns a string with leading and trailing spaces to
print(f"Padded Text: '{padded_text}'")- Prints the padded text.
print(f"Trimmed Text: '{padded_text.strip()}'")- Removes leading and trailing spaces using
.strip()and prints the trimmed text.
- Removes leading and trailing spaces using
9. Checking String Properties
alpha_numeric = "Python123"- Assigns
"Python123"toalpha_numeric.
- Assigns
print(f"Is '{alpha_numeric}' alphanumeric? {alpha_numeric.isalnum()}")- Checks if the string is alphanumeric using
.isalnum()and prints the result.
- Checks if the string is alphanumeric using
print(f"Is '{sample_text}' alphabetic? {sample_text.isalpha()}")- Checks if the string contains only letters using
.isalpha()and prints the result.
- Checks if the string contains only letters using
10. Iterating Through a String
print("Characters in string:")- Prints a message before iterating.
for char in sample_text: ...- Loops through each character in
sample_textand prints them.
- Loops through each character in
11. String Formatting
name = "Alice"- Assigns
"Alice"toname.
- Assigns
age = 25- Assigns
25toage.
- Assigns
print(f"Formatted string: My name is {name} and I am {age} years old.")- Prints a formatted string using f-strings.
12. Encoding and Decoding
encoded_text = sample_text.encode("utf-8")- Encodes
sample_textto UTF-8 and assigns it toencoded_text.
- Encodes
print(f"Encoded text: {encoded_text}")- Prints the encoded byte sequence.
decoded_text = encoded_text.decode("utf-8")- Decodes
encoded_textback to a string and assigns it todecoded_text.
- Decodes
print(f"Decoded text: {decoded_text}")- Prints the decoded text.
13. Palindrome Check
palindrome_candidate = "madam"- Assigns
"madam"topalindrome_candidate.
- Assigns
is_palindrome = palindrome_candidate == palindrome_candidate[::-1]- Checks if the string is equal to its reverse and assigns the result to
is_palindrome.
- Checks if the string is equal to its reverse and assigns the result to
print(f"Is '{palindrome_candidate}' a palindrome? {'Yes' if is_palindrome else 'No'}")- Prints whether the string is a palindrome.
14. Count Occurrences of a Character
char_to_count = 'l'- Assigns
'l'tochar_to_count.
- Assigns
print(f"Count of '{char_to_count}' in '{sample_text}': {sample_text.count(char_to_count)}")- Counts occurrences of
'l'insample_textusing.count()and prints the result.
- Counts occurrences of
15. String Concatenation
part1 = "Hello"- Assigns
"Hello"topart1.
- Assigns
part2 = "World"- Assigns
"World"topart2.
- Assigns
concatenated = part1 + ", " + part2 + "!"- Concatenates
part1,part2, and additional text.
- Concatenates
print(f"Concatenated String: {concatenated}")- Prints the concatenated string.
16. Running the Main Function
if _name_ == "_main_":- Checks if the script is being executed directly.
main()- Calls the
main()function to execute the program.
- Calls the
Comments
Post a Comment