Programming Articles - Page 3236 of 3366

How to create an empty list in Python?

Akshitha Mote
Updated on 17-Apr-2025 16:42:25

751 Views

In Python, list is one of the built-in data types. A Python list is a sequence of items separated by commas, enclosed in square brackets [ ]. The items in a Python list need not be of the same data type.  In this article, we will discuss different ways to create an empty list in Python. Using Square Brackets This is one of the simplest way to create an empty list to using square brackets[]. An empty list means the list has no elements at the time of creation, but we can add items to it later when needed. my_list=[] ... Read More

How to create Python dictionary from list of keys and values?

Akshitha Mote
Updated on 17-Apr-2025 16:41:25

24K+ Views

In Python, the dictionary is one of the built-in data types that stores the data in the form of key-value pairs. The pair of key-value is separated by a comma and enclosed within curly braces {}. The key and value within each pair are separated by a colon (:). Each key in a dictionary is unique and maps to a value. It is an unordered, mutable data. Creating dictionary from lists In Python, we can create a dictionary by using two separate lists. One list is considered as the keys, and the second one is considered as values. We ... Read More

In Python how to create dictionary from two lists?

Pythonic
Updated on 30-Jul-2019 22:30:21

443 Views

If L1 and L2 are list objects containing keys and respective values, following list comprehension syntax can be used to construct dictionary object. >>> L1 = ['a','b','c','d'] >>> L2 = [1,2,3,4] >>> d = {L1[k]:L2[k] for k in range(len(L1))} >>> d {'a': 1, 'b': 2, 'c': 3, 'd': 4}

How to convert an integer to an ASCII value in Python?

Akshitha Mote
Updated on 15-Apr-2025 14:44:38

2K+ Views

Converting Integer to ASCII Using chr() Function In Python, the chr() function converts an integer value to the corresponding ASCII (American Standard Code for Information Interchange) character only if the integer range lies between 0 and 127. The chr() function accepts Unicode values within the range of 0 to 1114111. If a value outside this range is provided, the function raises a ValueError. Example In the following example, we have converted integer values to ASCII values using chr() - print(chr(85)) print(chr(100)) print(chr(97)) Following is the output of the above code - U 100 a Example We can ... Read More

How to convert an integer to a unicode character in Python?

Akshitha Mote
Updated on 22-Jan-2025 14:30:09

4K+ Views

Unicode is a standardized character encoding that assigns a unique number to each character in most of the world's writing systems. Unicode separates the code points from the details of the encoding system. This permits a much wider range of characters up to four bytes. The Unicode character set incorporates the entirety of the ASCII character set as the first 127 characters. All ASCII characters have the same code points in both encodings. Techniques to Convert an Integer to a Character Following are the various techniques to convert an integer to a character in Python − ... Read More

How to convert a single character to its integer value in Python?

Sakshi Rayu
Updated on 02-May-2025 19:46:40

919 Views

In Python, the ord() function converts a given character to the corresponding ASCII (American Standard Code for Information Interchange) integer. The ord() function raises a TypeError if you pass a string value as a parameter. Converting a single alphabet to its integer In the following example, we have converted a character 'A' into its Unicode using the ord() function - my_str='A' result=ord(my_str) print("Unicode of 'A'-", result) Following is an output of the above code - Unicode of 'A'- 65 Printing Integer values of all the alphabets We can use the ord() function to print all the Unicode characters ... Read More

How we can create a dictionary from a given tuple in Python?

Pythonista
Updated on 25-Feb-2020 11:14:12

439 Views

We can use zip() function to produce an iterable from two tuple objects, each corresponding to key and value items and then use dict() function to form dictionary object>>> T1=('a','b','c','d') >>> T2=(1,2,3,4) >>> dict((x,y) for x,y in zip(t1,t2))Dictionary comprehension syntax can also be used to construct dictionary object from two tuples>>> d={k:v for (k,v) in zip(T1,T2)} >>> d {'a': 1, 'b': 2, 'c': 3, 'd': 4}

How to extract a group from a Java String that contains a Regex pattern

Arnab Chakraborty
Updated on 21-Jun-2020 06:30:13

317 Views

How to extract a group from a Java String that contains a Regex patternimport java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexTest {    public static void main(String[] args) {       Pattern pattern = Pattern.compile("fun");       Matcher matcher = pattern.matcher("Java is fun");       // using Matcher find(), group(), start() and end() methods       while (matcher.find()) {          System.out.println("Found the text \"" + matcher.group()             + "\" starting at " + matcher.start()             + " index and ending at index ... Read More

How to capture multiple matches in the same line in Java regex

Arnab Chakraborty
Updated on 20-Jun-2020 10:49:20

3K+ Views

Exampleimport java.util.regex.*; class PatternMatcher {    public static void main(String args[]) {       int count = 0;       // String to be scanned to find the pattern.       String content = "aaa bb aaa";       String string = "aaa";       // Create a Pattern object       Pattern p = Pattern.compile(string);       // get a matcher object       Matcher m = p.matcher(content);       while(m.find()) {          count++;          System.out.println("Match no:"+count);         ... Read More

Search and Replace with Java regular expressions

Sravani S
Updated on 26-Feb-2020 08:09:33

1K+ Views

Java provides the java.util.regex package for pattern matching with regular expressions. Java regular expressions are very similar to the Perl programming language and very easy to learn.A regular expression is a special sequence of characters that help you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data.The replaceFirst() and replaceAll() methods replace the text that matches a given regular expression. As their names indicate, replaceFirst replaces the first occurrence, and replaceAll replaces all occurrences.ExampleLive Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexMatches { ... Read More

Advertisements