PYTHON
PROGRAMMIN
G
Which
functions, is it
Events?
FUNCTIONS
OBJECTIVES
Students will learn about:
• In-built functions and methods in Python
program
▪String is a data type
used to store a
STRING
number of
characters.
For converting cases, the
following functions are used:
▪ stringname.upper( ): to
convert to uppercase
characters
▪ stringname.lower( ): to
convert to lowercase characters
Note: These functions work
perfectly in Python.
len( )
▪ It is used to return the length of string.
Ex-
Python program to find the number of characters in your
name .
name=str(input('Enter your name '))
print ('The number of character in your name is ',
len(name))
EXAMPLES-
Python has a set of built-in methods that you can use
on strings.
Note: All string methods returns new values. They do
not change the original string.
Finding the length of a string
mylist = "Hello Everyone"
x = len(mylist)
print(x)
Output: What is the output of the above code? -
Conversion of a string to
lowercase
txt = "Hello my FRIENDS"
x = txt.lower( )
print(x)
Output: hello my friends
Conversion of a string to
uppercase
txt = "Hello my FRIENDS"
x = txt.upper( )
print(x)
Strip Function
Remove spaces at the beginning and at the end
of the string:
txt = " apple "
x = txt.strip()
print(len(txt))
print(len(x))
Output: 9
5
Slicing[Finding substring]
You can return a range of characters by using the slice
syntax.
Get the characters from position 2 to position 5 (not
included):
b = "Hello, World!"
print(b[2:5])
Output: 1lo
Slice From the Start
Get the characters from the start to position 5 (not
included):
b = "Hello, World!"
print(b[:5])
Reverse of a string
Reverse the string "Hello World":
txt = "Hello World"
print(txt[::-1] )
Output: dlroW olleH
Extended Study Link:
Sort a string Python String Methods (w3school
Sort a Python String with sorted() s.com)
It results in a list
word = 'datagy'
sorted_word = sorted(word)
print(sorted_word)
Output: ['a','a','d','g','t','y']
random module
Python has a built-in module that you can
use to make random numbers.
The random module has a set of methods:
randint() Returns a random number
between the given range
random() Returns a random float
number between 0 and 1
uniform() Returns a random float Extra Information
number between two given
parameters
Click on the
links for more
information
Python Random randint( )
Definition and Use
The randint( ) method returns an integer number selected element from the specified range.
random.randint(start, stop)
Return a number between 10 and 50 (both included):
import random
print(random.randint(10, 50))
Thank
you