![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 27104 Articles for Server Side Programming
![Malhar Lathkar](https://www.tutorialspoint.com/assets/profiles/7345/profile/60_93814-1511173378.jpg)
2K+ 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'
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
3K+ Views
The following code using Python regex matches the given multiple words in the given stringExampleimport re s = "These are roses and lilies and orchids, but not marigolds or .." r = re.compile(r'\broses\b | \bmarigolds\b | \borchids\b', flags=re.I | re.X) print r.findall(s)OutputThis gives the output['roses', 'orchids', 'marigolds']
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
483 Views
There's no validate method as almost anything is a valid URL. There are some punctuation rules for splitting it up. Without any punctuation, you still have a valid URL.Depending on the situation, we use following methods.If you trust the data, and just want to verify if the protocol is HTTP, then urlparse is perfect.If you want to make the URL is actually a true URL, use the cumbersome and maniacal regexIf you want to make sure it's a real web address, use the following codeExampleimport urllib try: urllib.urlopen(url) except IOError: print "Not a real URL"Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
22K+ 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)
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
332 Views
The following code does the replacement in the given text file. After the replacement, the text is written to a new text file 'bar.txt'Examplef1 = open('foo.txt', 'r') f2 = open('bar.txt', 'w') for line in f1: print line f2.write(line.replace('Poetry', 'Prose')) f2 = open('bar.txt', 'r') for line in f2: print line, f1.close() f2.close()OutputThis gives the outputPoetry is often considered the oldest form of literature. Poetry today is usually written down, but is still sometimes performed. Prose is often considered the oldest form of literature. Prose today is usually written down, but is still sometimes performed.Read More
![Malhar Lathkar](https://www.tutorialspoint.com/assets/profiles/7345/profile/60_93814-1511173378.jpg)
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
![Malhar Lathkar](https://www.tutorialspoint.com/assets/profiles/7345/profile/60_93814-1511173378.jpg)
116 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
![Malhar Lathkar](https://www.tutorialspoint.com/assets/profiles/7345/profile/60_93814-1511173378.jpg)
7K+ 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
![Pythonic](https://www.tutorialspoint.com/assets/profiles/15007/profile/60_156531-1514381552.jpg)
5K+ Views
There are different ways to iterate through a tuple object. The for statement in Python has a variant which traverses a tuple till it is exhausted. It is equivalent to foreach statement in Java. Its syntax is −for var in tuple: stmt1 stmt2ExampleFollowing script will print all items in the listT = (10, 20, 30, 40, 50) for var in T: print (T.index(var), var)OutputThe output generated is −0 10 1 20 2 30 3 40 4 50Another approach is to iterate over range upto length of tuple, and use it as index of item in tupleExamplefor var in range(len(T)): ... Read More
![Malhar Lathkar](https://www.tutorialspoint.com/assets/profiles/7345/profile/60_93814-1511173378.jpg)
2K+ Views
List is an ordered sequence of elements. Individual element in list is accessed using index starting with 0 and goes up to length-1. If index goes beyond this range, IndexError exception is encountered.In following example, an infinite loop is used to pop one element at a time. As loop tries to go even after last element is popped, IndexError exception will be encountered. We trap it using try – except mechanism.a=[1,2,3] while True: try: b=a.pop() print (b) except (IndexError): break