23 Jan Python Prog 1 (Vrushali)

 

Imports and Function Definition

  1. def main():
    • Defines the main function, where the program logic resides.

1. Basic String Declaration

  1. sample_text = "Hello, World!"
    • Declares a string variable sample_text with the value "Hello, World!".
  2. print(f"Original Text: {sample_text}")
    • Prints the original text using an f-string for formatting.

2. String Length

  1. print(f"Length of string: {len(sample_text)}")
    • Uses the len() function to find and print the length of sample_text.

3. String Slicing

  1. print(f"First 5 characters: {sample_text[:5]}")
    • Slices the first 5 characters using [:5] and prints them.
  2. print(f"Last 5 characters: {sample_text[-5:]}")
    • Slices the last 5 characters using [-5:] and prints them.

4. Changing Case

  1. print(f"Uppercase: {sample_text.upper()}")
    • Converts the string to uppercase using .upper() and prints it.
  2. print(f"Lowercase: {sample_text.lower()}")
    • Converts the string to lowercase using .lower() and prints it.
  3. print(f"Title Case: {sample_text.title()}")
    • Converts the string to title case using .title() and prints it.
  4. print(f"Swap Case: {sample_text.swapcase()}")
    • Swaps the case of each character using .swapcase() and prints it.

5. Searching Within a String

  1. substring = "World"
    • Assigns "World" to the variable substring.
  2. print(f"Does the string contain '{substring}'? {'Yes' if substring in sample_text else 'No'}")
    • Checks if substring is in sample_text and prints the result.
  3. print(f"Index of '{substring}': {sample_text.find(substring)}")
    • Finds the starting index of substring using .find() and prints it.

6. Replacing Text

  1. replaced_text = sample_text.replace("World", "Python")
    • Replaces "World" with "Python" in sample_text and assigns it to replaced_text.
  2. print(f"Replaced Text: {replaced_text}")
    • Prints the modified string.

7. Splitting and Joining Strings

  1. words = sample_text.split(", ")
    • Splits sample_text at ", " and stores the resulting list in words.
  2. print(f"Split into words: {words}")
    • Prints the list of words.
  3. joined_text = " - ".join(words)
    • Joins the elements of words with " - " as a separator.
  4. print(f"Joined with ' - ': {joined_text}")
    • Prints the joined string.

8. Trimming Whitespace

  1. padded_text = " Hello, Python! "
    • Assigns a string with leading and trailing spaces to padded_text.
  2. print(f"Padded Text: '{padded_text}'")
    • Prints the padded text.
  3. print(f"Trimmed Text: '{padded_text.strip()}'")
    • Removes leading and trailing spaces using .strip() and prints the trimmed text.

9. Checking String Properties

  1. alpha_numeric = "Python123"
    • Assigns "Python123" to alpha_numeric.
  2. print(f"Is '{alpha_numeric}' alphanumeric? {alpha_numeric.isalnum()}")
    • Checks if the string is alphanumeric using .isalnum() and prints the result.
  3. print(f"Is '{sample_text}' alphabetic? {sample_text.isalpha()}")
    • Checks if the string contains only letters using .isalpha() and prints the result.

10. Iterating Through a String

  1. print("Characters in string:")
    • Prints a message before iterating.
  2. for char in sample_text: ...
    • Loops through each character in sample_text and prints them.

11. String Formatting

  1. name = "Alice"
    • Assigns "Alice" to name.
  2. age = 25
    • Assigns 25 to age.
  3. 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

  1. encoded_text = sample_text.encode("utf-8")
    • Encodes sample_text to UTF-8 and assigns it to encoded_text.
  2. print(f"Encoded text: {encoded_text}")
    • Prints the encoded byte sequence.
  3. decoded_text = encoded_text.decode("utf-8")
    • Decodes encoded_text back to a string and assigns it to decoded_text.
  4. print(f"Decoded text: {decoded_text}")
    • Prints the decoded text.

13. Palindrome Check

  1. palindrome_candidate = "madam"
    • Assigns "madam" to palindrome_candidate.
  2. is_palindrome = palindrome_candidate == palindrome_candidate[::-1]
    • Checks if the string is equal to its reverse and assigns the result to is_palindrome.
  3. 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

  1. char_to_count = 'l'
    • Assigns 'l' to char_to_count.
  2. print(f"Count of '{char_to_count}' in '{sample_text}': {sample_text.count(char_to_count)}")
    • Counts occurrences of 'l' in sample_text using .count() and prints the result.

15. String Concatenation

  1. part1 = "Hello"
    • Assigns "Hello" to part1.
  2. part2 = "World"
    • Assigns "World" to part2.
  3. concatenated = part1 + ", " + part2 + "!"
    • Concatenates part1, part2, and additional text.
  4. print(f"Concatenated String: {concatenated}")
    • Prints the concatenated string.

16. Running the Main Function

  1. if _name_ == "_main_":
    • Checks if the script is being executed directly.
  2. main()
    • Calls the main() function to execute the program.

Comments

Popular posts from this blog

Programming Notes by Atul Kalukhe