0% found this document useful (0 votes)
1 views

String Functions

The document provides an overview of string manipulation in Python, including indexing, slicing, and various built-in string methods such as len(), upper(), lower(), isdigit(), and more. It includes examples demonstrating how to use these methods and operators to manipulate strings effectively. Additionally, it covers string functions like split() and replace(), along with their outputs.

Uploaded by

Medha Joji
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)
1 views

String Functions

The document provides an overview of string manipulation in Python, including indexing, slicing, and various built-in string methods such as len(), upper(), lower(), isdigit(), and more. It includes examples demonstrating how to use these methods and operators to manipulate strings effectively. Additionally, it covers string functions like split() and replace(), along with their outputs.

Uploaded by

Medha Joji
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
You are on page 1/ 6

String in Python:

Its holds alphabets or digits or special charecters, starting with index (location) 0
and ending with n-1.
(or) start with -1 in reverse

A=”Welcome”
Print(A[-1])
A[0]=w
A[2]=l
A[-1]=e
A[-2]=m

Substring & slicing string:


str1="Welcome to learn Python"
Code Output
print(str1[0]) W

print(str1[3]) c

print(str1[-1]) n

print(str1[-2]) o

print(str1[3:6]) com

print(str1[3:7:2]) cm

print(str1[11:16]) learn

print(str1[11:16:4]) ln

print(str1[11:16:2]) lan

print(str1[::2]) Wloet er yhn

print(str1[::-2]) nhy re teolW

A="Welcome"
print(A+" to all") Welcome to all
print(5+2) 7
print("5"+"2") 52
print(5*3) 15
print("5"*3) 555
Sno Method Description Example OUTPUT

1 len() Says the length a=”Welcome” 7


of given string print(len(a))

2 upper() Converts into X=”aPPle” APPLE


capital letters print(X.upper())

3 lower() Converts into X=”AppLE” apple


small letters print(X.lower())

1 isdigit() Returns True if all a="34"


characters in the b="34.5"
string are digits c="hi"
d="hi10"
print(a.isdigit()) True
print(b.isdigit()) False
print(c.isdigit()) False
print(d.isdigit()) False

2 isalpha() Returns True if all a="34"


characters in the b="34.5"
string are alphabets c="hi"
d="hi10"
print(a.isalpha()) False
print(b.isalpha()) False
print(c.isalpha()) True
print(d.isalpha()) False

3 isalnum() Returns True if all a="34" True


characters in the b="34.5" False
string are alphabets c="hi" True
or digits or both d="hi10" True
print(a.isalnum())
print(b.isalnum())
print(c.isalnum())
print(d.isalnum())

4 isupper() Returns True if all a = "Hello World!"


characters in the b = "hello 123"
string are upper c = "MY NAME IS PETER"
letters print(a.isupper()) False
print(b.isupper()) False
print(c.isupper()) True

5 islower() Returns True if all a = "hellow world" True


characters in the b = "hello 123" True
string are lower c = "MY name is PETER" False
letters print(a.islower())
print(b.islower())
print(c.islower())

6 in used to check if a a="welcome to all"


value exists in a print("come" in a) True
sequence or not. print("COME" in a) False
Evaluates to true if
it finds a variable in
the specified
sequence and false
otherwise.
col="Mango is green color" yes
if "green" in col:
print("yes")

g
col="green" r
for x in col: e
print(x) e
n

7 not in 'not in' operator- a="welcome to all"


Evaluates to true if print("come" not in a)
it does not finds a print("COME" not in a)
variable in the False
specified sequence
and false otherwise True
Python supports the following built-in functions to manipulate string:

Syntax/ Method Description Example output


title( ) Returns a string in str1="my school"
title case
print(str1.title()) My School
(first letter each
word is caps)
swapcase( ) It will change case str1="uNiTeD ArAb
of every character emiRATes"
to its opposite case UnItEd aRaB
vice-versa. print(str1.swapcase()) EMIratES
replace(“char1”,”char2”) The replace str1="School"
function replaces
all occurrences of print(str1.replace('o','- Sch--l
char1 with char2. '))

txt = "I like bananas" I like apples

x = txt.replace("bananas",
"apples")

print(x)
split(“val”) str1 = "My school"
The split() method
splits a string into a str2 = str1.split(" ")
list.
print(str1) My school
You can specify the
separator, default print(str2) ["My","school"]
separator is any
whitespace. print(str2[0]) My

print(str2[1]) School
str1 = "this is wisdom"

str2 = str1.split("is")

print(str2[0]) th

print(str2[1]) “”

print(str2[2]) _w

print(str2[3]) dom
str1 = "My school is near My
to metro"
school
str2=str1.split(" ")
is
for i in str2:
near
print(i)
to

metro

count(“val”) str1 = "My school is near


Return the number to metro"
of times given value
appears in the n=str1.count(" ")
string:
print(n)
5
n=str1.count("o")

print(n)
4
n=str1.count("to")

print(n)
1

Reference link for Python String methods:

https://docs.python.org/2.5/lib/string-methods.html
Some more String Functions O/P

a="abc#mail.com" 3
print(a.find(“#"))
b="abcmail.com" (If the location is not found , it
print(b.find(“#")) returns -1)
-1
a="abc@xyz@mail@com" ['abc', 'xyz', 'mail', 'com']
print(a.split("@"))
b="abc@xyz@mail@com" ['abc', 'xyz@mail@com']
print(b.split("@",1))
c="abc@xyz@mail@com" ['abc', 'xyz', 'mail@com']
print(c.split("@",2))

You might also like