Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 116 - 2615_KEC
P. 116

    Program 9: To use various string built-in functions

                          Program9.py
                       File  Edit  Format    Run   Options   Window    Help

                       str = "Hello Orange Education"
                       print("Original string:", repr(str))

                       # Using len() function
                       length = len(str)
                       print("Length of string:", length)
                       # Using lower() function
                       lower_str = str.lower()
                       print("Lowercase:", lower_str)
                       # Using upper() function
                       upper_str = str.upper()
                       print("Uppercase:", upper_str)
                       # Using capitalize() function
                       capitalized_str = str.capitalize()
                       print("Capitalized:", capitalized_str)
                       # Using title() function
                       title_str = str.title()
                       print("Title Case:", title_str)

                       # Using swapcase() function
                       swapcase_str = str.swapcase()
                       print("Swap Case:", swapcase_str)

                       # Using casefold() function
                       casefold_str = str.casefold()
                       print("Casefolded:", casefold_str)

                       # Using find() function
                       index_hello = str.find("Orange")
                       print("Index of 'Hello':", index_hello)

                       # Using replace() function
                       replaced_str = str.replace("Hello ", "Welcome to ")
                       print("Replaced 'Hello' with 'Welcome to':", replaced_str)

                       # Using split() function
                       split_str = str.split()
                       print("Split string:", split_str)

                       # Using join() function
                       joined_str = "-".join(split_str)
                       print("Joined string with '-':", joined_str)

                       # Using strip() function
                       stripped_str = str.strip()
                       print("Stripped string:", repr(stripped_str))

                       114  Premium Edition-VIII

| 111 112 113 114 115 116 117 118 119 120 121 |