Manogna has Published 66 Answers

How to join two strings to convert to a single string in Python?

Manogna

Manogna

Updated on 12-Jun-2020 13:12:15

To join 2 strings in Python, we can use the concatenation operator, '+'. For example:str1 = "Hello" str2 = "World" str3 = str1 + str2 print str3This will give us the output:HelloWorldWe can also use str.join(seq) to join multiple strings together. For example:s = "-"; seq = ("a", "b", "c"); ... Read More

How do you test that a Python function throws an exception?

Manogna

Manogna

Updated on 12-Jun-2020 11:30:14

We write a unittest that fails only if a function doesn't throw an expected exception.We also test if a Python function throws an exception.For example, see the sample code we paste into the Python shell to test Python's type-safety:Exampleimport unittest class MyTestCase(unittest.TestCase):    def test_1_cannot_add_int_and_str(self):       with self.assertRaises(TypeError): ... Read More

What are the allowed characters in Python function names?

Manogna

Manogna

Updated on 12-Jun-2020 11:27:30

Python IdentifiersIdentifier is the name given to entities like class, functions, variables etc. in Python. It helps in knowing one entity from another.Rules for writing identifiersIdentifiers can be a combination of lowercase letters (a to z) or uppercase letters (A to Z) or digits (0 to 9) or an underscore ... Read More

What are default arguments in python?

Manogna

Manogna

Updated on 12-Jun-2020 11:25:42

Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default valueDefault arguments:ExamplePython has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument ... Read More

How to catch ValueError using Exception in Python?

Manogna

Manogna

Updated on 12-Jun-2020 07:56:10

A ValueError is used when a function receives a value that has the right type but an invalid value.The given code can be rewritten as follows to handle the exception and find its type.Exampleimport sys try: n = int('magnolia') except Exception as e: print e print sys.exc_typeOutputinvalid literal for int() ... Read More

How to check if text is “empty” (spaces, tabs, newlines) in Python?

Manogna

Manogna

Updated on 13-Feb-2020 09:59:53

The string can be checked by checking for occurrences of only whitespace characters. We can check if a string contains only whitespace characters using 2 methods. First is using method isspace(). exampleprint('Hello world'.isspace()) print('         '.isspace())OutputFalse TrueYou can also use regexes for the same result. For matching only ... Read More

How to check if a character is upper-case in Python?

Manogna

Manogna

Updated on 13-Feb-2020 09:58:52

To check if a character is upper-case, we can simply use isupper() function call on the said character.  exampleprint( 'Z'.isupper()) print( 'u'.isupper())OutputTrue FalseWe can also check it using range based if conditions.  exampledef check_upper(c):     if c >= 'A' and c <= 'Z':         return True     ... Read More

How exactly do Python functions return/yield objects?

Manogna

Manogna

Updated on 13-Feb-2020 06:46:59

The return statement causes a python function to exit and give back a value to its caller. The purpose of functions in general is to take in inputs and return something. A return statement, once executed, immediately terminates execution of a function, even if it is not the last statement ... Read More

Which is more fundamental between Python functions and Python object-methods?

Manogna

Manogna

Updated on 13-Feb-2020 06:40:52

A function is a callable object in Python, i.e. can be called using the call operator. However other objects can also emulate a function by implementing __call__method.  exampledef a(): pass # a() is an example of function print a print type(a)OutputC:/Users/TutorialsPoint/~.py <function a at 0x0000000005765C18> <type 'function'>A method is a ... Read More

How can we return a dictionary from a Python function?

Manogna

Manogna

Updated on 13-Feb-2020 06:29:29

There can be any number of ways we can return a dictionary from a python function. Consider the one given below.Example# This function returns a dictionary def foo():      d = dict();      d['str'] = "Tutorialspoint"      d['x']   = 50      return d print foo()Output{'x': ... Read More

1 2 3 4 5 6 7 Next
Advertisements