Found 11040 Articles for Python

Python Program to replace a word with asterisks in a sentence

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

2K+ 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


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

karthikeya Boyini
Updated on 20-Jun-2020 09:09:09


We want to calculate the ASCII sum for each word in a sentence and the sentence as a whole using map function and dictionaries. For example, if we have the sentence −"hi people of the world"The corresponding ASCII sums for the words would be : 209 645 213 321 552And their total would be : 1940.We can use the map function to find the ASCII value of each letter in a word using the ord function. Then using the sum function we can sum it up. For each word, we can repeat this process and get a final sum of ... Read More

Remove all duplicates from a given string in Python

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


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


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

Removing stop words with NLTK in Python

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


When computers process natural language, some extremely common words which would appear to be of little value in helping select documents matching a user need are excluded from the vocabulary entirely. These words are called stop words.For example, if you give the input sentence as −John is a person who takes care of the people around him.After stop word removal, you'll get the output −['John', 'person', 'takes', 'care', 'people', 'around', '.']NLTK has a collection of these stopwords which we can use to remove these from any given sentence. This is inside the NLTK.corpus module. We can use that to filter ... Read More

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

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


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

Sort the words in lexicographical order in Python

Samual Sam
Updated on 20-Jun-2020 08:30:41

3K+ Views

Sorting words in lexicographical order mean that we want to arrange them first by the first letter of the word. Then for the words whose first letter is the same, we arrange them within that group by the second letter and so on just like in a language's dictionary(not the data structure).Python has 2 functions, sort and sorted for this type of order, let us look at how and when to use each of these methods.In place sorting: when we want to sort the array/list in place, ie, changing the order in the current structure itself, we can use the ... Read More

Extracting email addresses using regular expressions in Python

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

4K+ 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 and my friend's email is jane_doe124@gmail.comWe should get the output − jane_doe124@gmail.comWe 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

Regex in Python to put spaces between words starting with capital letters

Samual Sam
Updated on 20-Jun-2020 08:43:36


The problem we are trying to solve here is to convert CamelCase to separate out words. We can solve this directly using regexes by finding all occurrences of a capital letter in the given string and put a space before it. We can use the sub method from the re module.For example, for the input string −AReallyLongVariableNameInJavaWe should get the output −A Really Long Variable Name In JavaWe can use "[A-Z]" regex to find all uppercase letters, then replace them with space and that letter again. We can implement it using the re package as follows −Example Live Demoimport re ... Read More