Page 107 - KEC Khaitan C8.5 Flipbook
P. 107
STRING BUILT-IN FUNCTIONS
Python includes the following built-in functions to manipulate strings:
len(): The len() function calculates and returns the length of a string supplied as an argument.
Syntax of using len() function is:
len(string_name)
lower(): The lower() function converts all uppercase letters to lowercase. Syntax of using lower()
function is:
string_name.lower()
upper(): The upper() function converts all lowercase letters to uppercase. Syntax of using
upper() function is:
string_name.upper()
capitalize(): The capitalize() function returns a string with the first character in capital. Syntax
of using capitalize() function is:
string_name.capitalize()
Program 11: To use various string built-in functions
Program11.py
File Edit Format Run Options Window Help
Output
str = 'Hello'
print(len(str)) 5
print('Hello'.lower()) hello
print('hello'.upper()) HELLO
print('hello'.capitalize())
Hello
SOME MORE PROGRAMS
Program 12: To test whether a given number is even or odd
Program12.py
File Edit Format Run Options Window Help
#Program to check whether the number is even or odd
number = int(input("Enter an integer: "))
if number % 2 == 0:
print("The number is even.") Output
else:
Enter an integer: 24
print("The number is odd.")
The number is even.
Control Structures in Python 105

