Page 103 - KEC Khaitan C8 Flipbook
P. 103

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))





                                                                                   Functions, String and List in Python  101
   98   99   100   101   102   103   104   105   106   107   108