0% found this document useful (0 votes)
3 views7 pages

Strings

The document provides an overview of various string manipulation methods in Python, including examples of functions like capitalize(), title(), lower(), upper(), count(), find(), index(), and more. It explains how to use these functions with sample inputs and outputs, demonstrating their effects on strings. Additionally, it covers methods for checking string properties such as isalnum(), isalpha(), isdigit(), and others.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Strings

The document provides an overview of various string manipulation methods in Python, including examples of functions like capitalize(), title(), lower(), upper(), count(), find(), index(), and more. It explains how to use these functions with sample inputs and outputs, demonstrating their effects on strings. Additionally, it covers methods for checking string properties such as isalnum(), isalpha(), isdigit(), and others.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Strings

Topic Examples
concatenation,
repetition,
membership
slicing
len()
capitalize() Important point to note - Capitalizes first letter and converts others
to lower case

x ="35 is good"
a = x.capitalize()
print(a)
35 is good

x = "Hi I AM AbhiRUP"
a = x.capitalize()
print(a)
Hi i am abhirup
title() txt =input("input: ")
a = txt.title()
print(a)

input: i am ranveer
I Am Ranveer

input: ran2veer
Ran2Veer
lower(),upper() txt =input("input: ")
a = txt.upper()
b=txt.lower()
print(a)
print(b)

Output:
input: Hello I am abhirup
HELLO I AM ABHIRUP
hello i am abhirup
count() a="heritage_"
b=a.count("e")
i=a.count("e",0,1)
c=a.count("z")
d=a.count("_")
l=a.count("ri")
print(i)
print(b)
print(c)
print(d)
print(l)
0
2
0
1
1
find() print ("This is how you use the find fucntion")
a = str(input("Enter a sentence - "))
if a==int:
print ("Invalid Input")
b = str(input("Enter the word you want to find - "))
x = a.find(b)
print(x)

This is how you use the find fucntion


Enter a sentence - Akshat is the best
Enter the word you want to find - Akshat
0

= RESTART: C:/Users/sp.lab35/Desktop/bhavya fhdsklfjsdkfs.py


This is how you use the find fucntion
Enter a sentence - Akshat
Enter the word you want to find - kshat
1

= RESTART: C:/Users/sp.lab35/Desktop/bhavya fhdsklfjsdkfs.py


This is how you use the find fucntion
Enter a sentence - Adit is so smart
Enter the word you want to find - s
7

= RESTART: C:/Users/sp.lab35/Desktop/bhavya fhdsklfjsdkfs.py


This is how you use the find fucntion
Enter a sentence - smart slow sigma smash
Enter the word you want to find - m
1

= RESTART: C:/Users/sp.lab35/Desktop/bhavya fhdsklfjsdkfs.py


This is how you use the find fucntion
Enter a sentence - smart slow sigma smash
Enter the word you want to find - sigma
11

= RESTART: C:/Users/sp.lab35/Desktop/bhavya fhdsklfjsdkfs.py


This is how you use the find fucntion
Enter a sentence - smart slow sigma smash
Enter the word you want to find - akshat
-1
index() variable = "apple,orange,banana"
print(variable.index("orange"0

SyntaxError: '(' was never closed


variable = "apple,orange,banana"
print(variable.index("orange")

SyntaxError: multiple statements found while compiling a single


statement
print(variable.index("orange"))

6
print(variable.index("a",2,8))

Traceback (most recent call last):


File "<pyshell#5>", line 1, in <module>
print(variable.index("a",2,8))
ValueError: substring not found
print(variable.index("a",2,5))

Traceback (most recent call last):


File "<pyshell#6>", line 1, in <module>
print(variable.index("a",2,5))
ValueError: substring not found
print(variable.index("a",2))

8
print(variable.index("z",2))

Traceback (most recent call last):


File "<pyshell#8>", line 1, in <module>
print(variable.index("z",2))
ValueError: substring not found
endswith() x = "Hello, This is a sentence."

a = x.endswith(".")
print(a)

print(x.endswith(",",5,6))
print(x.endswith("?"))

Output:
True
True
False
startswith()

isalnum() x= input("enter a string- ")


y= x.isalnum()

if y == True:
print("your string is alphanumeric")

else:
print("your string is not alphanumeric")
enter a string- HelloWorld123
your string is alphanumeric

enter a string- Hello World123


your string is not alphanumeric

enter a string- Hello_world123


your string is not alphanumeric

enter a string- 123


your string is alphanumeric
isalpha() x= input("enter a string- ")
y= x.isalpha()

if y == True:
print("your string is alpha")

else:
print("your string is not alpha")

Output:
enter a string- lol
your string is alpha

enter a string- l0l


your string is not alpha

enter a string- l ol
your string is not alpha

enter a string- LOL


your string is alpha

enter a string- Lol


your string is alpha
isdigit() x= input("enter a string- ")
y= x.isdigit()

if y == True:
print("your string is comprised of numbers")

else:
print("your string is not comprised of numbers")
enter a string- 12345

your string is comprised of numbers

enter a string- 17/7/24


your string is not comprised of numbers

enter a string- lol123


your string is not comprised of numbers

enter a string- 1.2


your string is not comprised of numbers
islower(),isupper x= input("enter a string- ")
(), y= x.islower()

if y == True:
print("your string is in lowercase")

else:
print("your string is not in lowercase")
enter a string- 1
your string is not in lowercase

= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- One
your string is not in lowercase

= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- one1
your string is in lowercase

= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- ONE
your string is not in lowercase

= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- one
your string is in lowercase

= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- on1e
your string is in lowercase
= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string-
your string is not in lowercase

= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- on*e
your string is in lowercase

x= input("enter a string- ")


y= x.isupper()

if y == True:
print("your string is in uppercase")

else:
print("your string is not in uppercase")

enter a string- HELLO


your string is in uppercase

= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- Hello
your string is not in uppercase
isspace() x= input(“Enter string-“)
y= x.isspace()

if y == True:
print("your string consists of only spaces")

else:
print("your string doesn't consist of just spaces")
Output:
enter a string-
your string consists of only spaces

= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string- a
your string doesn't consist of just spaces

= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
enter a string-
your string doesn't consist of just spaces

= RESTART: C:/Users/sp.lab35/Desktop/akshat.py
your string consists of only spaces
lstrip(),rstrip() string = " gourang x mihir x ranveer "
print(str(len(string)))
a = string.lstrip(" gornau x ")
b = string.rstrip()
print(a + ": " + str(len(a)))
print(b + ": " + str(len(b)))

output

47
mihir x ranveer : 23
gourang x mihir x ranveer: 39

strip() print('Definition of strip -')


print('The Strip() method in Python removes or truncates (The
truncate() method resizes the file to the given number of bytes. If the
size is not specified, the current position will be used) the given
characters from the beginning and the end of the original string. The
default behavior of the strip() method is to remove the whitespace
from the beginning and at the end of the string.')

print('************************************************************************
******')

print('Case 1')
txt1 = " FORTNITE "

print("The best game of all time is easily,", txt1)

print('************************************************************************
******')

print('Case 2')
txt1 = " FORTNITE "

y = txt1.strip()

print("The best game of all time is easily,",y)

print('************************************************************************
******')
replace() text = ('One fish two fish red fish blue fish')
new_text = text.replace('fish','cat', 2)
print(new_text)
text = ('One fish two fish red fish blue fish')
new_text = text.replace('fish','cat')
print(new_text)
text = ('One fish?two fish red fish blue fish')
new_text = text.replace('','*')
print(new_text)

Output:
One cat two cat red fish blue fish
One cat two cat red cat blue cat
*O*n*e* *f*i*s*h*?*t*w*o* *f*i*s*h* *r*e*d* *f*i*s*h* *b*l*u*e*
*f*i*s*h*
join() x=["apples","bananas","oranges"]
y=["kitkat","snickers","cadbury"]
print(x)
result='.'.join(x+y)
print(result)
result='.'.join(x).join(y)
print(result)

output
['apples', 'bananas', 'oranges']
apples.bananas.oranges.kitkat.snickers.cadbury
kitkatapples.bananas.orangessnickersapples.bananas.orangescadb
ury

=== Code Execution Successful ===

partition() x="apples bananas oranges"


print(x)
y =x.partition('bananas')
print(y)
print(y[0])
print(y[1])
print(y[2])

x='ilikepyhtonfunctions'
y=(x.partition(' '))
print(len(y[2]))

x="apples bananas oranges"


print(x)
y =x.partition('apples')
print(y)

output:
apples bananas oranges
('apples ', 'bananas', ' oranges')
apples
bananas
oranges
0
apples bananas oranges
('', 'apples', ' bananas oranges')
split() x='Hello World. How are we?'
y=x.split()
print(y)

x1='Hello World. How are we?'


y1=x1.split('.')
print(y1)

x1='Hello World. How are we?'


y1=x1.split(' ',2)
print(y1)

Output
['Hello', 'World.', 'How', 'are', 'we?']
['Hello World', ' How are we?']
['Hello', 'World.', 'How', 'are', 'we?']
chr(), ord()

You might also like