Mohd Mohtashim

Mohd Mohtashim

185 Articles Published

Articles by Mohd Mohtashim

Page 2 of 19

Accessing Values of Strings in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 2K+ Views

Python does not support a character type; these are treated as strings of length one, thus also considered a substring.ExampleTo access substrings, use the square brackets for slicing along with the index or indices to obtain your substring. For example −#!/usr/bin/python var1 = 'Hello World!' var2 = "Python Programming" print "var1[0]: ", var1[0] print "var2[1:5]: ", var2[1:5]OutputWhen the above code is executed, it produces the following result −var1[0]: H var2[1:5]: ytho

Read More

Updating Strings in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 3K+ Views

You can "update" an existing string by (re)assigning a variable to another string. The new value can be related to its previous value or to a completely different string altogether. For example −Example#!/usr/bin/python var1 = 'Hello World!' print "Updated String :- ", var1[:6] + 'Python'OutputWhen the above code is executed, it produces the following result −Updated String :- Hello Python

Read More

Triple Quotes in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 7K+ Views

Python's triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters.The syntax for triple quotes consists of three consecutive single or double quotes.Example#!/usr/bin/python para_str = """this is a long string that is made up of several lines and non-printable characters such as TAB ( \t ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ ], or just a NEWLINE within the variable assignment will also show up. """ print para_strOutputWhen the above code is executed, ...

Read More

Unicode String in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 3K+ Views

Normal strings in Python are stored internally as 8-bit ASCII, while Unicode strings are stored as 16-bit Unicode. This allows for a more varied set of characters, including special characters from most languages in the world. I'll restrict my treatment of Unicode strings to the following −Example#!/usr/bin/python print u'Hello, world!'OutputWhen the above code is executed, it produces the following result −Hello, world! As you can see, Unicode strings use the prefix u, just as raw strings use the prefix r.

Read More

Delete Tuple Elements in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 8K+ Views

Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded.To explicitly remove an entire tuple, just use the del statement.Example#!/usr/bin/python tup = ('physics', 'chemistry', 1997, 2000); print tup; del tup; print "After deleting tup : "; print tup;OutputThis produces the following result. Note an exception raised, this is because after del tup tuple does not exist any more −('physics', 'chemistry', 1997, 2000) After deleting tup : Traceback (most recent call last): File "test.py", line 9, in print tup; NameError: name 'tup' is not defined

Read More

No Enclosing Delimiters in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 333 Views

Any set of multiple objects, comma-separated, written without identifying symbols, i.e., brackets for lists, parentheses for tuples, etc., default to tuples, as indicated in these short examples −Example#!/usr/bin/python print 'abc', -4.24e93, 18+6.6j, 'xyz'; x, y = 1, 2; print "Value of x , y : ", x,y;OutputWhen the above code is executed, it produces the following result −abc -4.24e+93 (18+6.6j) xyz Value of x , y : 1 2

Read More

Properties of Dictionary Keys in Python

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 5K+ Views

Dictionary values have no restrictions. They can be any arbitrary Python object, either standard objects or user-defined objects. However, same is not true for the keys.There are two important points to remember about dictionary keys −More than one entry per key not allowed. Which means no duplicate key is allowed. When duplicate keys encountered during assignment, the last assignment wins.ExampleFollowing is a simple example −#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'} print "dict['Name']: ", dict['Name']OutputWhen the above code is executed, it produces the following result −dict['Name']: Manni Keys must be immutable. Which means you can use strings, numbers ...

Read More

The Match Operator in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 2K+ Views

The match operator m// in Perl, is used to match a string or statement to a regular expression. For example, to match the character sequence "foo" against the scalar $bar, you might use a statement like this −Example#!/usr/bin/perl $bar = "This is foo and again foo"; if ($bar =~ /foo/) {    print "First time is matching";    } else {    print "First time is not matching"; } $bar = "foo"; if ($bar =~ /foo/) {    print "Second time is matching";    } else {    print "Second time is not matching"; }When above program is executed, it ...

Read More

Matching Only Once in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 322 Views

There is a simpler version of the match operator in Perl - the ?PATTERN? operator. This is basically identical to the m// operator except that it only matches once within the string you are searching between each call to reset.For example, you can use this to get the first and last elements within a list −Example#!/usr/bin/perl @list = qw/food foosball subeo footnote terfoot canic footbrdige/; foreach (@list) {    $first = $1 if /(foo.*?)/;    $last = $1 if /(foo.*)/; } print "First: $first, Last: $last";When the above program is executed, it produces the following result −First: foo, Last: footbrdige

Read More

The Substitution Operator in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 3K+ Views

The substitution operator s/// in Perl is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is −s/PATTERN/REPLACEMENT/;The PATTERN is the regular expression for the text that we are looking for. The REPLACEMENT is a specification for the text or regular expression that we want to use to replace the found text with. For example, we can replace all occurrences of dog with cat using the following regular expression −Example#/user/bin/perl $string = "The cat sat on the mat"; $string =~ s/cat/dog/; print "$string";When the above program is ...

Read More
Showing 11–20 of 185 articles
« Prev 1 2 3 4 5 19 Next »
Advertisements