Showing posts from January, 2024


  def greet_with (name, location): print ( f"hello { name } " ) print ( f"How is the weather in { location } " ) greet_with( "Harry" , "Inala" )

Convert String to List and List to String

Convert String to List   my_string = "hello world" method_1 = list (my_string) method_2 = my_string.split() print (method_1) print (method_2) The result will be: ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] ['hello', 'world'] Convert List to String my_list = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] string_from_list = "".join(my_list) If your list contains elements other than strings (e.g., numbers), you need to convert them to strings first using the map() function or a list comprehension. string_from_numbers = '' .join( map ( str , my_list_of_numbers))


  In Python, range is a built-in function that is used to generate a sequence of numbers. It is commonly used in for loops to iterate a specific number of times or to create sequences of numbers. The basic syntax of the range function is as follows: range (stop) range (start, stop) range (start, stop, step) start (optional): The starting value of the sequence. If not specified, it defaults to 0 . stop : The exclusive upper limit of the sequence. The sequence will stop before reaching this value. step (optional): The step or the difference between each number in the sequence. If not specified, it defaults to 1 . Here are a few examples to illustrate the usage of the range function: Using range with a Stop Value: for i in range ( 5 ): print (i) Output: 0 1 2 3 4 In this example, range(5) generates a sequence of numbers from 0 to 4 . Using range with Start and Stop Values: for i in range ( 2 , 8 ): print (i) Output: 2 3 4 5 6 7 Here, range(2, 8) generates a seque