Found 9316 Articles for Python

Python program to Uppercase selective indices

Vikram Chiluka
Updated on 27-Jan-2023 12:22:26
In this article, we will learn a python program to convert the string elements to uppercase for the given selective indices. Methods Used The following are the various methods to accomplish this task − Using For Loop and upper() function Using List Comprehension Using index() & join() functions Example Assume we have taken an input string and a list containing index values. We will now convert the characters of an input string to uppercase at those given indices and print the resultant string. Input inputString = 'hello tutorialspoint python' indexList = [0, 6, 8, 10, 15, ... Read More

Python Program to Test if string contains element from list

Vikram Chiluka
Updated on 27-Jan-2023 12:28:22
In this article, we will learn how to check if a string contains an element from a list in python. Methods Used Using Nested For Loops Using List Comprehension Using any() function Using the find() function Example Assume we have taken an input string and input list. We will now check whether the input string contains at least any one input list element. Input inputString = "tutorialspoint is a best learning platform for coding" inputList = ['hello', 'tutorialspoint', 'python'] Output YES, the string contains elements from the input list In the above example, the ... Read More

Python Program to Test if any set element exists in List

Vikram Chiluka
Updated on 27-Jan-2023 12:19:16
In this article, we will learn how to check if any set element exists in the list in python. Methods Used Using any() function Using Bitwise & operator Using Counter(), filter() and lambda functions Example Assume we have taken an input set and input list. We will now check whether any input set element exists in the input list using the above methods. Input inputSet = {4, 8, 1, 3, 5, 7} inputList = [7, 15, 20] Output Checking whether any set element present in the input list: True In the above example, 7 exists ... Read More

Python Program to Split a String by Custom Lengths

Vikram Chiluka
Updated on 27-Jan-2023 12:08:51
In this article, we will learn how to split a string by custom lengths in python. Methods Used The following are the various methods to accomplish this task − Using for loop and slicing Using List Comprehension, join() and next() functions Example Assume we have taken an input string and custom lengths list. We will now split an input string by input custom lengths using the above methods. Input inputString = 'hitutorialspoint' customLengths = [4, 1, 6, 5] Output ['hitu', 't', 'orials', 'point'] In the above example, firstly the input list is split based on the ... Read More

Python Program to Sort A List Of Names By Last Name

Vikram Chiluka
Updated on 27-Jan-2023 12:05:17
In this article, we will learn a python program to sort a list of names by last name. Methods Used Using lambda and sorted() functions Using lambda and sort() functions Example Assume we have taken an input list containing string elements. We will now sort the list elements in ascending order of the last names alphabetically using the above methods. Input Input List: ['sachin tendulkar', 'suresh raina', 'hardik pandya'] Output Input List after sorting by last names: ['hardik pandya', 'suresh raina', 'sachin tendulkar'] The last names of input list elements are tendulkar, raina, pandya i.e t, ... Read More

Python Program to Replace all words except the given word

Vikram Chiluka
Updated on 27-Jan-2023 12:03:24
In this article, we will learn how to replace all words except the given word in a string in python. Methods Used The following are the various methods to accomplish this task − Using For Loop, split() & join() Functions Using List Comprehension Example Assume we have taken an input string, input word, and the input character with which to be replaced. We will now replace all the words in an input string with the given input replace character expect the given word using the above methods. Input inputString = 'hello tutorialspoint python codes' inputWord = "tutorialspoint" ... Read More

Python Program to Remove numbers with repeating digits

Vikram Chiluka
Updated on 27-Jan-2023 12:02:20
In this article, we will learn how to remove numbers with repeating digits in python. Methods Used The following are the various methods to accomplish this task − Using list comprehension and set() function Using re module Using the Counter() function Example Assume we have taken an input list containing numbers. We will now remove all the numbers from the list containing repeating digits and return the resultant list using the above-mentioned methods. Input inputList = [3352, 8135, 661, 7893, 99] Output [8135, 7893] In the above input list, in the 1st element 3352, 3 ... Read More

Python program to remove K length words in String

Vikram Chiluka
Updated on 27-Jan-2023 12:00:36
In this article, we will learn a python program to remove K-length words in String. Methods Used The following are the various methods to accomplish this task − Using split(), join(), len() and list comprehension Using filter(), lambda(), split() and len() functions Using split(), join(), remove() and len() functions Example Assume we have taken an input string and k length. We will now remove all the given k-length words from the input string using the above methods. Input inputString = "hello tutorialspoint python codes" k_length = 5 Output Resultant string after removing 5 length words: tutorialspoint ... Read More

Python Program to Remove all digits before given Number

Vikram Chiluka
Updated on 27-Jan-2023 11:59:04
In this article, we will learn how to remove all digits before a given number in python. Methods Used The following are the various methods to accomplish this task − Using List Comprehension, split(), enumerate() & index() Functions Using re module Using slicing, index() & replace() functions Example Assume we have taken an input string and an input number. We will now remove all the digits before the input number from an input string. Input inputString = 'hello 6 8 10 tutorialspoint 15 python codes' inputNumber = 10 Output Resultant string after removing digits before input number{ ... Read More

Python Program to Random uppercase in Strings

Vikram Chiluka
Updated on 27-Jan-2023 11:57:31
In this article, we will learn how to convert characters to uppercase in Strings Randomly using python. Methods Used The following are the various methods to accomplish this task − Using join(), choice(), lower(), upper() functions. Using random.randInt() and random.sample() functions. Using map(), choice(), zip() functions Example Assume we have taken an input string. We will now randomly convert the characters of an input string to uppercase using the above methods. Input inputString = 'tutorialspoint' Output String after converting chars lower or uppercase randomly: tUToriaLsPOINT In this example, We are converting characters of an input ... Read More
1 2 3 4 5 6 7 ... 932 Next
Advertisements