Python dictionary get() Method

Description

The method get() returns a value for the given key. If key is not available then returns default value None.

Syntax

Following is the syntax for get() method −

dict.get(key, default=None)

Parameters

  • key — This is the Key to be searched in the dictionary.
  • default — This is the Value to be returned in case key does not exist.

Return Value

This method return a value for the given key. If key is not available, then returns default value None.

Example

The following example shows the usage of get() method.

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 27}

print "Value : %s" %  dict.get('Age')
print "Value : %s" %  dict.get('Sex', "Never")

When we run above program, it produces following result −

Value : 27
Value : Never

 

SOURCE: http://www.tutorialspoint.com/python/dictionary_get.htm

Python String strip() Method

Description

The method strip() returns a copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters).

Syntax

Following is the syntax for strip() method −

str.strip([chars]);

Parameters

  • chars — The characters to be removed from beginning or end of the string.

Return Value

This method returns a copy of the string in which all chars have been stripped from the beginning and the end of the string.

Example

The following example shows the usage of strip() method.

#!/usr/bin/python

str = "0000000this is string example....wow!!!0000000";
print str.strip( '0' )

When we run above program, it produces following result −

this is string example....wow!!!


SOURCE: http://www.tutorialspoint.com/python/string_strip.htm