Found 10400 Articles for Python

Python program to split and join a string?

Sarika Singh
Updated on 23-Nov-2022 08:08:52

10K+ Views

We'll learn how to split and join a string in Python in this article. Python defines strings as a collection of characters enclosed in one, two, or three quotations. When a string is split, it is divided into smaller strings using a specific delimiter. A set of one or more characters used to define a boundary is known as a delimiter. Anything could serve as a delimiter. The comma (, ), semicolon (;), tab (t), space (), and pipe (|) are the most widely used delimiters. Everywhere the specified delimiter appears in a given string, we must split it, and ... Read More

Python program to multiply all numbers in the list?

Sarika Singh
Updated on 23-Nov-2022 08:03:08

26K+ Views

You will understand how to multiply every number in a list in Python in this article using various methods. A list is an ordered collection of values that are enclosed in square brackets. List contains values known as items that can be retrieved by their unique index. We'll create a function that multiplies each value in the list and outputs the result. The result of multiplying all the values is a single thing. For instance, the result will be 36 for the list [3, 2, 6]. We will go over the various methods for calculating the sum of all the ... Read More

Python program to Remove and print every third from list until it becomes empty?

Sarika Singh
Updated on 23-Nov-2022 08:05:38

2K+ Views

In this article, we will learn how to remove and print every third element or item from the list until it becomes empty using Python Program. First we create a list, the index of the starting address is 0 and the position of the first third element is 2 and need to traverse till the list becomes empty and another important work to do every time we have to find the index of the next third element and print the value and after that reduce the length of the list. Input-Output Scenario Following is the input and its output scenario ... Read More

Python Program to replace a word with asterisks in a sentence

Sarika Singh
Updated on 23-Nov-2022 08:07:48

3K+ Views

The use of the symbol * (asterisks) in writing or printing as a reference mark, a sign that some letters or words have been omitted, a way to indicate a possible but unproven linguistic form, or for other arbitrary purposes. In this article we will discuss different ways to replace a word with asterisks in a sentence in Python. Input-Output Scenarios Following is an input and its output scenario of replacing a word in a sentence with asterisks − Input: Welcome to the TutorialsPoint family. Output: Welcome to the TutorialsPoint ****** We can see in the above scenario that ... Read More

Map function and Lambda expression in Python to replace characters

Samual Sam
Updated on 20-Jun-2020 09:08:20

595 Views

We want to replace a character a1 with a character a2 and a2 with a1. For example, For the input string, "puporials toinp"and characters p and t, we want the end string to look like −"tutorials point"For this we can use map function and lambdas to make the replacement. The map(lambda, input) function iterates over each item passed to it(in form of iterable input) and apply the lambda expression to it. So we can use it as follows −Example Live Demodef replaceUsingMapAndLambda(sent, a1, a2): # We create a lambda that only works if we input a1 or a2 and swaps them. ... Read More

Map function and Dictionary in Python to sum ASCII values

Yaswanth Varma
Updated on 17-Jun-2025 16:11:58

733 Views

In this article, we are going to learn how to use the map() function along with dictionaries to add the ASCII values of characters in the string. The Python built-in ord() function returns the ASCII integer value of a character. Additionally, dictionaries (dict) in Python are used to store key-value pairs. We use them to associate strings with their total ASCII values, making it easy to store and retrieve the result. Before diving into the examples, let's have a quick look at the Python map() function and the Python ord() function. Python map() Function The Python map() function ... Read More

Remove all duplicates from a given string in Python

Samual Sam
Updated on 20-Jun-2020 09:10:40

680 Views

To remove all duplicates from a string in python, we need to first split the string by spaces so that we have each word in an array. Then there are multiple ways to remove duplicates.We can remove duplicates by first converting all words to lowercase, then sorting them and finally picking only the unique ones. For example, Examplesent = "Hi my name is John Doe John Doe is my name" # Seperate out each word words = sent.split(" ") # Convert all words to lowercase words = map(lambda x:x.lower(), words) # Sort the words in order words.sort() ... Read More

Tokenize text using NLTK in python

karthikeya Boyini
Updated on 20-Jun-2020 08:27:52

823 Views

Given a character sequence and a defined document unit, tokenization is the task of chopping it up into pieces, called tokens, perhaps at the same time throwing away certain characters, such as punctuation. In the context of nltk and python, it is simply the process of putting each token in a list so that instead of iterating over each letter at a time, we can iterate over a token.For example, given the input string −Hi man, how have you been?We should get the output −['Hi', 'man', ', ', 'how', 'have', 'you', 'been', '?']We can tokenize this text using the word_tokenize ... Read More

Python program to sort out words of the sentence in ascending order

karthikeya Boyini
Updated on 20-Jun-2020 08:29:57

1K+ Views

In order to sort the words of a sentence in ascending order, we first need to split the sentence into words using space as the splitting point. For simplicity, we'll only be splitting on space and let the punctuation be there. We can use replace or regex to remove that as well.Once we split the sentence, we can sort the words lexicographically(like in a language dictionary) using either sort or sorted methods depending on whether we want to sort the array in place or sort it, then return a new array.In place sorting: when we want to sort the array/list ... Read More

Extracting email addresses using regular expressions in Python

karthikeya Boyini
Updated on 20-Jun-2020 08:41:36

5K+ Views

Email addresses are pretty complex and do not have a standard being followed all over the world which makes it difficult to identify an email in a regex. The RFC 5322 specifies the format of an email address. We'll use this format to extract email addresses from the text.For example, for a given input string −Hi my name is John and email address is [email protected] and my friend's email is [email protected] should get the output −[email protected] [email protected] can use the following regex for exatraction −[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+We can extract the email addresses using the find all method from re module. For example, ... Read More

Advertisements