Python Articles - Page 951 of 1048

Map function and Dictionary in Python to sum ASCII values

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

808 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

770 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

866 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

Removing stop words with NLTK in Python

Yaswanth Varma
Updated on 28-Aug-2025 12:55:26

820 Views

In NLP(Natural Language Processing), stop words are the words that are filtered out before or after processing text data, such as "is", "and", "a" etc. These words do not add meaning to the text and can be removed to improve the efficiency. The Natural Language Toolkit (NLTK) is the python library that provides the easy to use interface and the tools for text processing such as tokenization and stop word removal. In this article, we will explore how to remove stop words using NLTK. NLTK Stop Words Before going to use the NLTK stop words, we have ... 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

Sort the words in lexicographical order in Python

Yaswanth Varma
Updated on 28-Aug-2025 12:45:34

3K+ Views

Lexicographical order is similar way the words are arranged in a dictionary. In this article, we are going to learn how to sort the words in lexicographical order, which means arranging them in alphabetical order (A-Z). Python provides built-in methods like split() and sorted() to perform this operation. Using Python split() Method The Python split() method is used to split a string by using the specified separator. This separator is a delimiter string and can be a comma, a full stop, or any other character to split a string. Syntax Following is the syntax of Python str.split() method ... 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 john.doe@somecompany.co.uk and my friend's email is jane_doe124@gmail.comWe should get the output −john.doe@somecompany.co.uk 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

Find all the patterns of \"10+1\" in a given string using Python Regex

Sumana Challa
Updated on 09-May-2025 12:17:34

231 Views

The term "10plus1" is a specific pattern in a binary string that starts with a digit '1' followed by at least one or more '0' and ending with a '1'. In regular expressions this pattern is represented as - 10+ 1 Using re.findall() The re.findall() method accepts a pattern and a string as parameters, finds the given pattern in the entire string, and returns all the matches in the form of a list.To find the patterns of "10+1" in a given string, we just need to pass the same as pattern to the findall() method along with the string.Example ... Read More

How can I remove the same element in the list by Python

Sumana Challa
Updated on 09-May-2025 10:30:40

3K+ Views

A list is a built-in Python data structure that is used to store an ordered collection of items of different data types. It often occurs that lists contain duplicate values, i.e., the same element repeating multiple times, which causes data inaccuracies. In this article, we will discuss the approaches that can be used to remove the repeated elements from a list. Using set() Using List Comprehension Using a For Loop Using Dictionary fromkeys() Using set() Function The set() function accepts an iterable ... Read More

Can someone help me fix this Python Program?

Arnab Chakraborty
Updated on 24-Jun-2020 07:26:01

124 Views

The first problem u are getting in the bold portion is due to non-indent block, put one indentation there.second problem is name variable is not definedfollowing is the corrected one -print ("Come-on in. Need help with any bags?") bag=input ('(1) Yes please  (2) Nah, thanks   (3) Ill get em later  TYPE THE NUMBER ONLY') if bag == ('1'): print ("Ok, ill be right there!") if bag == ('2'): print ("Okee, see ya inside. Heh, how rude of me? I'm Daniel by the way, ya?") name="Daniel" print (name + ": Um, Names " + name) print ("Dan: K, nice too ... Read More

Advertisements