0% found this document useful (0 votes)
5 views4 pages

Python String and Lambda Examples

The document contains Python code examples demonstrating string manipulation methods, indexing, and slicing. It also includes a lambda function to swap two numbers without using a third variable. Various string operations such as replacement, capitalization, counting characters, and checking types are illustrated with their outputs.

Uploaded by

raoaarav09
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Python String and Lambda Examples

The document contains Python code examples demonstrating string manipulation methods, indexing, and slicing. It also includes a lambda function to swap two numbers without using a third variable. Various string operations such as replacement, capitalization, counting characters, and checking types are illustrated with their outputs.

Uploaded by

raoaarav09
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Sawan kumar B-TECH IT

text = "hello world 123"

print("Original String:", text)

print("replace('world','Python'):", [Link]("world", "Python"))

print("capitalize():", [Link]())

print("split():", [Link]())

print("count('l'):", [Link]("l"))

print("title():", [Link]())

print("len():", len(text))

print("upper():", [Link]())

print("lower():", [Link]())

print("'hello'.isalpha():", "hello".isalpha())

print("[Link]():", [Link]())

print("'hello123'.isalnum():", "hello123".isalnum())

print("[Link]():", [Link]())

print("'-'.join(['a','b','c']):", "-".join(['a','b','c']))

print(" nd('world'):", text. nd("world"))

print(" nd('Python'):", text. nd("Python")) # not found → -1

print("'123'.isdigit():", "123".isdigit())

print("'hello'.isdigit():", "hello".isdigit())

print("' '.isspace():", " ".isspace())

print("'hello'.isspace():", "hello".isspace())

OUTPUT :-

Original String: hello world 123

replace('world','Python'): hello Python 123

capitalize(): Hello world 123

split(): ['hello', 'world', '123']

count('l'): 3

title(): Hello World 123


fi
fi
fi
fi
len(): 15

upper(): HELLO WORLD 123

lower(): hello world 123

'hello'.isalpha(): True

[Link](): False

'hello123'.isalnum(): True

[Link](): False

'-'.join(['a','b','c']): a-b-c

nd('world'): 6

nd('Python'): -1

'123'.isdigit(): True

'hello'.isdigit(): False

' '.isspace(): True

'hello'.isspace(): False

text = "HELLO"

print("Original String:", text)

print("\nForward Indexing:")

print("text[0]:", text[0])

print("text[1]:", text[1])

print("text[2]:", text[2])

print("text[3]:", text[3])

print("text[4]:", text[4])

print("\nReverse Indexing:")

print("text[-1]:", text[-1])

print("text[-2]:", text[-2])
fi
fi
print("text[-3]:", text[-3])

print("text[-4]:", text[-4])

print("text[-5]:", text[-5])

print("\nSlicing Examples:")

print("text[0:3]:", text[0:3])

print("text[1:4]:", text[1:4])

print("text[:3]:", text[:3])

print("text[2:]:", text[2:])

print("text[:]:", text[:])

print("text[::2]:", text[::2])

print("text[::-1]:", text[::-1])

OUTPUT

Original String: HELLO

Forward Indexing:

text[0]: H

text[1]: E

text[2]: L

text[3]: L

text[4]: O

Reverse Indexing:

text[-1]: O

text[-2]: L

text[-3]: L

text[-4]: E

text[-5]: H

Slicing Examples:
text[0:3]: HEL

text[1:4]: ELL

text[:3]: HEL

text[2:]: LLO

text[:]: HELLO

text[::2]: HLO

text[::-1]: OLLEH

# Swap two numbers without using a third variable (with lambda)

# Lambda function for swapping

swap = lambda a, b: (b, a)

x, y = 10, 20

print("Before Swapping: x =", x, ", y =", y)

x, y = swap(x, y) # Swap using lambda

print("After Swapping: x =", x, ", y =", y)

OUTPUT

Before Swapping: x = 10 , y = 20

After Swapping: x = 20 , y = 10

You might also like