The example at the end of the documentation topic explains how:
import imp import sys def __import__(name, globals = None, locals = None, fromlist = None): # Fast path: see if the module has already been imported. try: return sys.modules[name] except KeyError: pass # If any of the following calls raises an exception, # there 's a problem we can' t handle-- let the caller handle it. fp, pathname, description = imp.find_module(name) try: return imp.load_module(name, fp, pathname, description) finally: # Since we may exit via an exception, close fp explicitly. if fp: fp.close()
Here is what i finally came up with to get the function i wanted back out from a dottedname
from string import join def dotsplit(dottedname): module = join(dottedname.split('.')[: -1], '.') function = dottedname.split('.')[-1] return module, function def load(dottedname): mod, func = dotsplit(dottedname) try: mod = __import__(mod, globals(), locals(), [func, ], -1) return getattr(mod, func) except(ImportError, AttributeError): return dottedname
A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement.  (They are also run if the file is executed as a script.),In fact function definitions are also ‘statements’ that are ‘executed’; the execution of a module-level function definition adds the function name to the module’s global namespace.,This does not add the names of the functions defined in fibo directly to the current namespace (see Python Scopes and Namespaces for more details); it only adds the module name fibo there. Using the module name you can access the functions:,you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file:
# Fibonacci numbers module def fib(n): # write Fibonacci series up to n a, b = 0, 1 while a < n: print(a, end = ' ') a, b = b, a + b print() def fib2(n): # return Fibonacci series up to n result =  a, b = 0, 1 while a < n: result.append(a) a, b = b, a + b return result
>>> import fibo
>>> fibo.fib(1000) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 >>> fibo.fib2(100)[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >>> fibo.__name__ 'fibo'
>>> fib = fibo.fib >>> fib(500) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>> from fibo import fib, fib2 >>> fib(500) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>> from fibo import * >>> fib(500) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
In this tutorial you will learn to create, format, modify and delete strings in Python. Also, you will be introduced to various string operations and functions.,There are many operations that can be performed with strings which makes it one of the most used data types in Python.,Strings can be created by enclosing characters inside a single quote or double-quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings.,Python String Operations Concatenation of Two or More Strings Iterating Through String String Membership Test Built-in functions to Work with Python
Strings can be created by enclosing characters inside a single quote or double-quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings.
# defining strings in Python # all of the following are equivalent my_string = 'Hello' print(my_string) my_string = "Hello" print(my_string) my_string = '' 'Hello' '' print(my_string) # triple quotes string can extend multiple lines my_string = "" "Hello, welcome to the world of Python "" " print(my_string)
When you run the program, the output will be:
Hello Hello Hello Hello, welcome to the world of Python
The index of
-1 refers to the last item,
-2 to the second last item and so on. We can access a range of items in a string by using the slicing operator
#Accessing string characters in Python str = 'programiz' print('str = ', str) #first character print('str = ', str) #last character print('str[-1] = ', str[-1]) #slicing 2n d to 5 th character print('str[1:5] = ', str[1: 5]) #slicing 6 th to 2n d last character print('str[5:-2] = ', str[5: -2])
If we try to access an index out of the range or use numbers other than an integer, we will get errors.
# index must be in range >>> my_string ... IndexError: string index out of range # index must be an integer >>> my_string[1.5] ... TypeError: string indices must be integers
Strings are immutable. This means that elements of a string cannot be changed once they have been assigned. We can simply reassign different strings to the same name.
>>> my_string = 'programiz' >>> my_string = 'a' ... TypeError: 'str' object does not support item assignment >>> my_string = 'Python' >>> my_string 'Python'
Last Updated : 28 Jul, 2022
A Computer Science portal for geeks
String with the use of Single Quotes: Welcome to the Geeks World String with the use of Double Quotes: I 'm a Geek String with the use of Triple Quotes: I 'm a Geek and I live in a world of "Geeks" Creating a multiline String: Geeks For Life