Get Last 4 Characters of a String in Python

Malhar Lathkar
Updated on 20-Feb-2020 08:12:13

3K+ Views

The slice operator in Python takes two operands. First operand is the beginning of slice. The index is counted from left by default. A negative operand starts counting from end. Second operand is the index of last character in slice. If omitted, slice goes upto end.We want last four characters. Hence we count beginning of position from end by -4 and if we omit second operand, it will go to end.>>> string = "Thanks. I am fine" >>> string[-4:] 'fine'

Assign a Reference to a Variable in Python

Malhar Lathkar
Updated on 20-Feb-2020 08:11:01

1K+ Views

Concept of variable in Python is different from C/C++. In C/C++, variable is a named location in memory. Even if value of one is assigned to another, it creates a copy in another location.int x=5; int y=x;For example in C++, the & operator returns address of the declared variable.cout

Unpack String of Integers to Complex Numbers in Python

Malhar Lathkar
Updated on 20-Feb-2020 08:09:49

236 Views

A string contains two integers separated by comma. It is first split in a list of two strings having digits.>>> s="1,2".split(",") >>> s ['1', '2']Two items are then converted to integers and used as arguments for complex() function>>> complex(int(s[0]), int(s[1])) (1+2j)This results in unpacking of string of integers in a complex number

Iterate Through a Python List of Tuples

Malhar Lathkar
Updated on 20-Feb-2020 08:07:37

8K+ Views

Easiest way is to employ two nested for loops. Outer loop fetches each tuple and inner loop traverses each item from the tuple. Inner print() function end=’ ‘ to print all items in a tuple in one line. Another print() introduces new line after each tuple.ExampleL=[(1,2,3), (4,5,6), (7,8,9,10)] for x in L:   for y in x:     print(y, end=' ')   print()Output1 2 3 4 5 6 7 8 9 10

Read and Write Unicode UTF-8 Files in Python

Rajendra Dharmkar
Updated on 20-Feb-2020 07:59:19

29K+ Views

The io module is now recommended and is compatible with Python 3's open syntax: The following code is used to read and write to unicode(UTF-8) files in PythonExampleimport io with io.open(filename,'r',encoding='utf8') as f:     text = f.read() # process Unicode text with io.open(filename,'w',encoding='utf8') as f:     f.write(text)

Get the Number of Capture Groups in Python Regular Expression

Rajendra Dharmkar
Updated on 20-Feb-2020 07:57:19

2K+ Views

The following code gets the number of captured groups using Python regex in given stringExampleimport re m = re.match(r"(\d)(\d)(\d)", "632") print len(m.groups())OutputThis gives the output3

Extract Anchor Tags from Webpage using Python Regular Expressions

Rajendra Dharmkar
Updated on 20-Feb-2020 07:56:17

222 Views

The following code extracts all tags in the given stringExampleimport re rex = re.compile(r'[\]') l = "this is text1 hi this is text2" print rex.findall(l)Output['', '']

Use Wildcard in Python Regular Expression

Rajendra Dharmkar
Updated on 20-Feb-2020 07:49:42

2K+ Views

The following code uses the Python regex .()dot character for wildcard which stands for any character other than newline.Exampleimport re rex = re.compile('th.s') l = "this, thus, just, then" print rex.findall(l)OutputThis gives the output['this', 'thus']

Find All Adverbs and Their Positions in Text Using Python Regular Expression

Rajendra Dharmkar
Updated on 20-Feb-2020 07:40:37

468 Views

As per Python documentationIf one wants more information about all matches of a pattern than the matched text, finditer() is useful as it provides match objects instead of strings. If one was a writer who wanted to find all of the adverbs and their positions in some text, he or she would use finditer() in the following manner −>>> text = "He was carefully disguised but captured quickly by police." >>> for m in re.finditer(r"\w+ly", text): ...     print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0))) 07-16: carefully 40-47: quickly

Extract Numbers from Text Using Python Regular Expression

Rajendra Dharmkar
Updated on 20-Feb-2020 07:17:09

2K+ Views

If we want to extract all numbers/digits  individually from given text we use the following regexExampleimport re s = '12345 abcdf 67' result=re.findall(r'\d', s) print resultOutput['1', '2', '3', '4', '5', '6', '7']If we want to extract groups of numbers/digits from given text we use the following regexExampleimport re s = '12345 abcdf 67' result=re.findall(r'\d+', s) print resultOutput['12345', '67']

Advertisements