Executing String Code Using exec()In this section, we will demonstrate how to execute Python code stored in a string using the built-in exec() function.Example 1: Using the Built-in exec() Methoddefex···
Extracting URLs from a Given String Using Regular ExpressionsIn this section, we will use a regular expression to extract URLs from a string.Example:importredefFind(string):#findall()findsallsubstring···
Determine the Length of a Given StringIn this section, we will define a string and check its length using different methods.Example 1: Using the Built-in len() Methodstr="runoob"print(len(st···
Check if a Substring Exists in a Given StringIn this section, we will define a string and check whether a specified substring exists within the string.Exampledefcheck(string,sub_str):if(string.find(su···
Remove a Character from a Specified Position in a StringIn this section, we will define a string and remove a character at a specified position.Exampletest_str="PMEve"We will remove the thir···
Finding the Largest Element in a ListIn this section, we will define a list of numbers and find the largest element in the list.ExampleInput:list1=[10,20,4]Output:20Example 1: Using Sortinglist1=[10,2···
Finding the Smallest Element in a ListIn this section, we will define a list of numbers and find the smallest element in the list.ExampleInput:list1=[10,20,4]Output:4Example 1: Using Sortinglist1=[10,···
Calculating the Product of Elements in a ListIn this section, we will define a list of numbers and compute the product of its elements.ExampleInput:list1=[1,2,3]Output:6Calculation:1*2*3=6Example 1: U···
Calculating the Sum of Elements in a ListIn this section, we will define a list of numbers and compute the sum of its elements.ExampleInput:[12,15,3,10]Output:40Example 1: Using a Looptotal=0list1=[11···
Counting Occurrences of an Element in a ListIn this section, we will learn how to count the number of times a specific element appears in a list.ExampleInput:lst=[15,6,7,10,12,20,10,28,10]x=10Output:3···
Cloning a List in PythonIn this section, we will learn how to copy the elements of one list to another list.Example 1: Using Slicingdefclone_list(li1):li_copy=li1[:]returnli_copyli1=[4,8,2,1
Removing Duplicates from a ListIn this chapter, we will learn how to remove duplicate elements from a list.Key Points:Python Set: A set is an unordered collection of unique elements.Python List: A lis···
Define a List and Clear It Using the clear() MethodThe following Python code demonstrates how to clear a list using the clear() method.Example 1RUNOOB=[6,0,4,1]print('Beforeclearing:',RUNOOB)#···
Define a List and Check if an Element is in the ListThis Python code demonstrates various methods for checking if a specific element exists in a list.Example 1test_list=[1,6,3,5,3,4]print("Checki···
Define a List and Reverse ItThis Python code demonstrates how to define a list and reverse its elements using different methods.Example:Before reversing:list=[10,11,12,13,14,15]After reversing:[15,14,···
Define a List and Swap Two Elements at Specified PositionsThis Python code demonstrates how to define a list and swap two elements at specified positions. Several implementations are provided, each ac···