Hi Guys!!
Today I am discuss Python Function and String inbuilt method.
Python’s functions are first-class objects. We can
Today I am discuss Python Function and String inbuilt method.
Python’s functions are first-class objects. We can
- assign them to variables,
- store them in data structures,
- pass them as arguments to other functions, and
- even return them as values from other functions.
# 1. Functions Are Objects. we can assign it to another variable, just like any other object(example strings, lists)
def welcomeMsg(name):
return 'Welcome {} !!'.format(name)
receive = welcomeMsg
# The above line does’t call the function. It takes the function object
reference and creates a second name,
# We can execute the method by calling receive:
print(receive("Saurabh"))
# Function objects and their names are two separate things. We can delete the functions’ original name and still call the function, since another name still points to it.
del welcomeMsg
#print(welcomeMsg("Saurabh")) # O/P : NameError: "name 'greet' is not
defined"
print(receive("Saurabh")) # O/P :Welcome Saurabh !!
# 2. Functions Can Be Stored in Data Structures. we can add functions to a list:
funcs = [len, str.isdigit, str.upper]
for f in funcs:
print(f, f('Hello World!'))
Output :
<built-in function len> 12
<method 'isdigit' of 'str' objects> False
<method 'upper' of 'str' objects> HELLO WORLD!
# 3. Functions Can Be Passed to Other Functions
# 3. Functions Can Be Passed to Other Functions
def accept(func):
msg = func('Roark')
print(msg)
def welcomeMsg(name):
return 'Welcome {} !!'.format(name)
def leave(name):
return "Good Bye {}!".format(name)
accept(welcomeMsg)
accept(leave)
Output :
Welcome Roark !!
Good Bye Roark!
The ability to pass function objects as arguments to other functions is powerful. It allows us to abstract away and pass around behaviours. In above example, accept function stays the same but we can influence its output by passing in different behaviours.
# 4. Functions Can Be Nested. They are often called nested functions or inner functions.
def num1(x):
def num2(y):
return x * y
return num2
res = num1(10)
print(res(5)) # O/P :- 50
A string is a sequence of characters. The built-in string class in Python represents strings based upon the Unicode international character set. All the string methods always return new values and do not change or manipulate the original string.
Below are some famous method under String object in Python
- center( ) : center aligns a string
- count( ) returns the count or the number of times a particular value appears in a string
- find() method returns the lowest index of a particular substring in a string
- swapcase() method returns a copy of the string with all its uppercase letters converted into lower case and vice versa
- The swapcase() method returns a copy of the string with all its uppercase letters
- startswith( ) and endswith( ) method returns True/False for given string
- split( ) spilt the string as list with given delimiter bydefault is space is delimeter
- capitalize( ) method capitalizes only the first character of a string.
- upper( ) method capitalizes all the letters of the string.
- string.title( ) method capitalizes all the first letters of the given string.
- The zfill() method adds zeros(0) at the beginning of the string
- The strip() method returns a copy of the string with the leading and trailing characters removed.Default character to be removed is whitespace
All above method example you can find from my github page
# 1. center( ) : center aligns a string
words = "Saurabh"
print(words.center(15, " "))
# 2. count( ) returns the count or the number of times a particular value appears in a string
str1 = "Kuamr Sharma Saurabh Kumar Sharma Saurabh kumar Saurabh Sharma"
print(str1.count('Saurabh'))
print(str1.count('Saurabh',9,36))
# 3. The find()method returns the lowest index of a particular substring in a string
print(str1.find('Saurabh'))
print(str1.rfind('Saurabh')) # rfind highest index
# 4. The swapcase() method returns a copy of the string with all its uppercase letters
# converted into lower case and vice versa
str2 = "Saurabh Kumar Sharma"
print(str2.swapcase())
# 5. startswith( ) and endswith( )
print(str2.startswith('Kumar'))
print(str2.endswith("Sharma"))
# 6. split( )
fruits = "Apple, Mango, Grapes, Banana"
print(fruits.split())
print(fruits.split(","))
print(fruits.split(",", maxsplit=2))
.......See my github page for more details.
Thanks
Happy Coding !!!!
Saurabh Sharma
Happy Coding !!!!
Saurabh Sharma