Python OE 28022025
Python OE 28022025
embership
M
‘H’ in ‘Hello’ #True
#space separated string
st = ‘’
for c in ‘python’:
st = st + c + ‘ ‘
#Count hashtags in a post Or display hashtags in a post.
Built-in Functions on Strings: Some common built-instring methods are listed
below with examples for a few of them. Explore python string documentation for
details.
en(
l ‘Hello World’
) #length of a string
11
ax/min
m
max(‘AB’, ‘BC’, ‘AC’)
min(‘ABCD’, ‘BCD, ‘CA’)
f ind/rfind
colors = ‘green, red, blue, red’
colors.find(‘red’) #1, first location, -1 if not found
colors.rfind(‘red’) #3
lower, upper
‘heLLo’.lower()
‘heLLo’.upper()
r eplace
‘abcdefab’.replace(‘ab’, ‘aa’) #’aacdefab’
s trip
‘
hello world ‘.strip() #’hello world
s plit
colors =’red, green, blue’
colors.split(‘,’) #[‘red’, ‘ green’, ‘ blue’]
j oin
‘+’.join(‘abc’) #’a+b+c’
s tartswith/endswith
‘helloooo’.startswith(‘he’) #True
Function:
- A function is a reusable block of code that performs a specific task.
- Functions make code modular, readable, and reusable.
- Functions can accept 0 or more arguments/parameters. A function may or
may not have a return statement.
Basic Syntax
def function_name():
<statements>
Example:
def greet():
print("Hello! Welcome to Python.")
greet()
#function call.
Function parameter and return value:
- Finding GC content in a DNA sequence
def gc_content(dna):
g_count = dna.count("G")
c_count = dna.count("C")
return (g_count + c_count) / len(dna) * 100
print(gc_content("ATGCGTACGTAGCGT"))
print(profit_percentage(200, 250))
print(bacterial_growth(10, 5))
rint(is_prime(11))
p
print(is_prime(15))
Exercises:
4. Write a program to extract and print all digits from a given string.
5. Write a function that takes a sentence as input parameter and returns the
number of words in the sentence.