Found 34489 Articles for Programming

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

Naveen Singh
Updated on 25-Feb-2020 11:16:05

1K+ Views

ASCII character associated to an integer is obtained by chr() function. The argument for this function can be any number between 0 to 0xffff>>> chr(0xaa) 'ª' >>> chr(0xff) 'ÿ' >>> chr(200) 'È' >>> chr(122) 'z'

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

Naveen Singh
Updated on 09-Sep-2023 15:26:00

3K+ Views

Python library's chr() function converts Unicode character associated to any integer which is between 0 an 0x10ffff.>>> chr(36) '$' >>> chr(97) 'a' >>> chr(81) 'Q'

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

Naveen Singh
Updated on 25-Feb-2020 11:14:47

512 Views

Each character is associated with an ASCII value which is a unique number. It is obtained by ord() function.>>> ord('A') 65 >>> ord('+') 43 >>> ord(' ')

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

Naveen Singh
Updated on 25-Feb-2020 11:14:12

319 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 do we convert a string to a set in Python?

Naveen Singh
Updated on 25-Feb-2020 11:12:57

535 Views

Python’s standard library contains built-in function set() which converts an iterable to set. A set object doesn’t contain repeated items. So, if a string contains any character more than once, that character appears only once in the set object. Again, the characters may not appear in the same sequence as in the string as set() function has its own hashing mechanism>>> set("hello") {'l', 'h', 'o', 'e'}

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

Naveen Singh
Updated on 21-Jun-2020 06:30:13

224 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

Naveen Singh
Updated on 20-Jun-2020 10:49:20

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

Naveen Singh
Updated on 26-Feb-2020 08:09:33

885 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

Regular Expressions syntax in Java Regex

Naveen Singh
Updated on 30-Jul-2019 22:30:21

191 Views

Following is a simple program demonstrating how to use regular expression in Java. Java Regex Characters

Regex named groups in Java

Naveen Singh
Updated on 30-Jul-2019 22:30:21

184 Views

Java Regex Capturing Groups

Advertisements