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

Python String Methods Overview

Uploaded by

sehajsethi2010
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)
45 views4 pages

Python String Methods Overview

Uploaded by

sehajsethi2010
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

Chapter – 9 : Strings in Python

(To be copied in notebook)


1. join()
Returns a string in which the string elements have been joined by a string separator
Example :
str1=‘12345’
s=‘-’
[Link](str) gives 1-2-3-4-5

2. swapcase() converts the cases of character in a string (lower to upper and vice versa)
Example :
str=‘PyTHoN@123’
[Link]() gives pYthOn@123

3. partition(separator) breaks the string by given string(separator). It gives a tuple.


Example :
str1=‘xyz@[Link]’
str2=[Link](‘@’)
print(str2) gives ( ‘xyz’, ‘@’, ‘[Link]’)
str3=[Link](‘ ‘)
print(str3) gives (‘xyz@[Link]’, ’ ‘, ’ ‘)

4. count() This method returns number of occurrances of a substring in a string.


Example :
Str1=“ this is my school. It is in subroto park”
Str2=“is”
[Link](Str2) gives 3

5. ord() this function returns the equivalent ASCII value of a character.


ASCII values are assigned to each character ranges from 0 to 255
A=65, Z=90 a=97 z=122 0(zero)=48 9=57 etc
Example :
ch=‘b’
print( ord(ch)) gives 98

6. chr() this function returns the equivalent character for given ASCII value
Example :
print( chr(97)) gives ‘a’

1|Page
7. find() The method find() determines if string str occurs in string, or in a substring of string if
starting index beg and ending index end are given.
[Link](str, beg,end)
Parameters
str -- This specifies the string to be searched.
beg -- This is the starting index, by default its 0.
end -- This is the ending index, by default its equal to the length of the string.
This method returns index if found and -1 otherwise.
Example :

8. rfind() The method is same as find() but searches from end.


[Link](str1, beg, end)
Example :

9. center() Returns a string with the original string centered to a total of width columns and filled
with fillchar in columns that do not have characters.

2|Page
10. endswith() Checks if string ends with any substring(given). Returns True if so otherwise False.
You can set beginning index and end index also.
[Link](str1, beg, end)
Example :
str=“ The air force school”
str1 = “ol"
print ([Link](str1)) gives True

11. startswith() Checks whether string starts with substring.


Example :
str=“python”
print( [Link](“py”)) gives True

12. index() Same as find but raises error if substring not found.
Example :
str=“ He is my best friend”
print([Link](“mine’)) raises an error
print([Link](“be”)) gives 9

13. rindex() Same as index() but it checks from end of string.


Example :
str=“python”
print([Link](“mine’)) raises error

14. ljust() String will be left justified.


Example :
str=“best friend”
print([Link](15,’*’) best friend****

15. rjust() Same as ljust() , String will be right justified.


Example :
str=“python”
print( [Link](10, ’*’)) gives ****python

3|Page
16. zfill() returns string left padded with zeros to a total of specified width.
Example :
str=“python”
print([Link](10)) gives 0000python

17. max() Returns the highest alphabetical character( having highest ASCII value).
Example :
str=“best friend”
print(max(str)) gives ‘t’

18. min() Returns the lowest alphabetical character( having lowest ASCII value).
Example :
str=“python”
print( min(str)) gives ‘h’

19. title() returns string in title case


Example :
str=“the air force school”
print([Link]()) gives The Air Force School

String Constants:
Many modules in python library provide both functions and constants for dealing with various
elements of the program.
The string module provides various constants that are useful for various purposes.
We need to import string module before using any of the constants of this module with the
following statement:
import string

Constants in string module :


1. string.ascii_uppercase
2. string.ascii_lowercase
3. string.ascii_letters
4. [Link]
5. [Link]
6. [Link]
7. [Link]
8. [Link]
9. [Link]

4|Page

You might also like