Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
85 views

Python - String Functions String

This document summarizes Python string functions. It defines what a string is, how to initialize and access characters in a string. It describes various string operations like slicing, concatenation, replication and escape sequences. It also discusses many built-in string functions like len(), capitalize(), upper(), lower(), title() and others for checking types, counting, indexing and splitting strings.

Uploaded by

Amuthapriyaa GL
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Python - String Functions String

This document summarizes Python string functions. It defines what a string is, how to initialize and access characters in a string. It describes various string operations like slicing, concatenation, replication and escape sequences. It also discusses many built-in string functions like len(), capitalize(), upper(), lower(), title() and others for checking types, counting, indexing and splitting strings.

Uploaded by

Amuthapriyaa GL
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

PYTHON – STRING FUNCTIONS

String:
Set of unicode characters
Includes Letters,Numbers, Special Characters or
combination of these types
Representation
with in single / double/triple single/triple double
quotes
ex: ‘apple’ “apple” ‘ ‘apple’’ “““apple”””
‘A140’
Initialization: Syntax: Var_name=‘text’
Ex: Name=“India”
I n d i a
0 1 2 3 5

Accessing : var_name[index]
Str1=“india”
print(str1)
print(str1[3])
print(str1[1:3]
)
String Operations
• Slicing
– Used to access a range of string
– strvar[start:finish-1]
• Ex: print(str1[1:3]

• Concatenation
– Used to combine the strings
– var-=strvar1+strvar2+….+strvarN
• Replication
– Used to repeat a string N number of times
(*)
– Var1=Strvar*N
String Operations
• Escape sequence
– Control characters, not displayed on screen
– Performs specific task
– Used to design the output
– ‘\n’ ‘\a’‘\t’
• Format method
– Used to format / design output
– Syntax: format(value , format specifier)
– Ex:alignment
str1="hai"
print(format(str1,'<20'))

print(format(str1,'>20'))
String Built in Functions
1. len()len(strvar)
Returns No. of chars
2. capitalize()
strvar.capitalize()
Capitalize the string (turn first char into upper case)
3. upper() strvar.upper()
Convert lower case into upper case
4. lower() strvar.lower()
Convert upper case into lower case
5. title() strvar.title()
Convert the string into title case
s1="hai" length=len(s1)
print("\t\t Length is=\t\t",length)
print("\t\tCapital=\t\t",s1.capitalize())
s2="HAI"
print("\t\tLower=\t\t",s2.lower ())

print("\t\tUpperr=\t\t",s1.upper())
s3="i am an indian"
print("\t\tTitle=\t\t",s3.title())
String Built in Functions
1. isalnum()
strvar.isalnum()
returns true if the string contains alphabet,numeric
2. isalpha()
strvar.isalpha()
returns true if the string contains only alphabet
3. isdigit()
strvar.isdigit()
returns true if the string contains only digits
4. isupper()
strvar.isupper()
returns true if string is in upper case
s1="A130"
s2="apple"
s3="1234"
print("Alnum\t\t=",s1.isalnum())

print("Alnum\t\t=",s2.isalnum())
print("Alpha\t\t=",s2.isalpha())

print("Alpha\t\t=",s1.isalpha())
print("Digit\t\t=",s1.isdigit())
print("Digit\t\t=",s3.isdigit())
String Built in Functions
1. istitle()
strvar.count(sub(str)
)
returns true if string is
in title case
2. isspace()
strvar.isspace()
returns true if string is
a spaec

Ex:
String Built in Functions
1. count()
strvar.count(substr)
returns total number of occurences of the given
sub string
2. index()
strvar.index(sub[start,end])
returns the lowest index of the substring present
in the string (first occurrence)
String Built in Functions
1. endswith()
strvar.endswith(substr)
strvar.endswith(substr,start,end)
returns a boolean value
True- if strvar ends with the substr
False- otherwise
2. find()
strvar.find(substr)
returns the lowest index of the substring
(first occurrence)
3. split() strvar.split(str)
returns the list of all words in the string using str as a
separator
s1="String string String string String"
print(s1.count("string"))
print(s1.count("String"))
print(s1.endswith("string"))
print(s1.endswith("String",0,27))
print(s1.endswith("String",0,35))
print(s1.find("string"))

print(s1.split(' '))

You might also like