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

unit-3 Strings (1)

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

unit-3 Strings (1)

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

Unit-3

STRINGS
How to change or delete a string?

Strings are immutable. This


means that elements of a string cannot be
changed once it has been assigned. We
can simply reassign different strings to the
same name.
>>> my_string = 'programiz'
>>> my_string[5] = 'a'
TypeError: 'str' object does not support
item assignment.
REPRESENTATION OF STRING
>>> s = “Hello Python”
This is how Python would index the string:

Backward Indexing

Forward Indexing
STRINGS - Example

OUT PUT
STRINGS

How to access characters in a string?

Python allows negative indexing for


its sequences.

The index of -1 refers to the last item,


-2 to the second last item and so on. We
can access a range of items in a string by
using the slicing operator (colon).
OUT PUT
SLICING STRINGS EXAMPLES
For example:
>>>“Program”[3:5] >>>p = “Program”
will result in: >>>p [4:]
‘gr ’ ‘ram’
>>>“Program”[3:6]
will yield: >>>p = “Program”
‘gra’ >>>p [3:6]
‘gra’
>>>p = “Program”
>>>p [:4]
‘Prog’
More Functionality of String

>>> len(“Sainik School”)


Finding Length of string
13
>>>print(“Sainik” + “School”)
Sainik School String Concatenation
>>>print(“A” * 4 )
AAAA
String Repeat
>>>”sa” in “sainik”
True
>>>”pr” in “computer” Substring Tests
False
String Methods
String Methods

In Python, a method is a function that is


defined with respect to a particular object.
Syntax:

object.method(arguments)
For Example: the first position
>>>name=“Classic” where “s” appears
>>>name.find(“s”)
3
String object String Method Method Argument
Built in Methods
1. capitalize() Method

Capitalizes first letter of string

First letter capitalization


2. lstrip() & 3. rstrip() Methods

lstrip() method is used to remove left


padded spaces in a given string
>>>name1=“ a “
>>>name1.lstrip() Removing left spaces
‘a ‘

rstrip() method is used to remove right


padded spaces in a given string
>>>name1.rstrip()
‘ a’
Removing right spaces
4. strip() Method

strip() method is used to remove left


and right padded spaces in a given string
>>>name1=“ a “
>>>name1.strip()
‘a‘

Removing left and right spaces for a given string


5. lower() Method

lower() method is used to convert


given string in to lower case.
>>>name1=“ SAINIK “
>>>name1.lower()
sainik

Converting in to lower case


6. upper() Method

upper() method is used to convert


given string in to upper case.
>>>name1=“ sainik “
>>>name1.upper()
SAINIK

Converting in to upper case


7. title() Method

title() method is used to convert given


string in to title case. Every first chatacter of
word of a given string is converted to title
case.
>>>name1=“ praveen murigeppa jigajinni“
>>>name1.title()
Praveen Murigeppa Jigajinni

In every word first letter is capitalized


8. swapcase() Method

swapcase() method is toggle the case.


Meaining upper to lower and lower to upper
case.
>>>name1=“ PrAvEeN“
>>>name1.swapcase()
pRaVeEn

Every character case is changed


9. ljust() Method

ljust() method is used to add spaces to


the left side of the given string
>>>name1=“ Jigajinni“
>>>name1.ljust(15)
‘Jigajinni ’

right side padded with spaces

Note: string length is 9 left side and 6


spaces added to the right side of string
10. rjust() Method

rjust() method is used to add spaces to


the right side of the given string
>>>name1=“ Jigajinni“
>>>name1.rjust(15)
‘ Jigajinni’

Left side padded with spaces

Note: string length is 9 right side and 6


spaces added to the left side of string
11. center(width, fillchar) Method

The method center() returns centered


in a string of length width. Padding is done
using the specified fillchar. Default filler is a
space.

Centered string
12. zfill() Method

zfill() method is used to fill the zero to a


given string
13. find() Method

find() method is used to find a


perticular character or string in a given
string.
>>> name1="praveen"
>>> name1.find("e")
4

e is present at 4th location (first appearance)


in a given string
14. count() Method

count() method is used to the number


of times character or string appears in a
given string.

>>>name1=“praveen“
>>>name1.count(“e”)
2

2 times e appears in a given string


15. stratswith() Method

startswith() method is used check


string start with particular string or not

>>>name1=“praveen“
>>>name1.startswith(“a”)
False

Given string not starting with “a”


16. endswith() Method

endswith() method is used check string


ends with particular string or not

>>>name1=“praveen“
>>>name1.endswith(“en”)
True

Given string ends with “en”


17. isdigit() Method

isdigit() method is used check string is


digit (number) or not and returns Boolean
value true or false.
>>>name2=“123”
>>>name2.isdigit() Given string is
True number
>>name1=“123keyboard“
>>name1.isdigit()
False

Given string not number so false


18. isnumeric() Method

isnumeric() is similar to isdigit()


method and is used check string is digit
(number) or not and returns Boolean value
true or false.
>>>name2=“123” Given string is
>>>name2.isnumeric() number
True
>>name1=“123keyboard“
>>name1.isnumeric()
False
Given string not number so false
19. isdecimal() Method

isnumeric(),isdigit() and isdecimal()


methods are used to check string is digit
(number) or not and returns Boolean value
true or false.
>>>name2=“123” Given string is
>>>name2.decimal() number
True
>>name1=“123keyboard“
>>name1.isnumeric()
False
Given string not number so false
20. isalpha() Method

isalpha() method is used check string is


digit or not and returns Boolean value true or
false.
>>>name2=“123” Given string does
>>>name2.isalpha() not contain string
False
Given string not
>>name1=“123computer“
string it contains
>>name1.isalpha()
digits
False
>>>name3=“Keynoard”
>>>Name3.isaplpha() It’s a string
True
21. isalnum() Method

isalnum() method is used check string is


alpha numeric string or not.

>>>name2=“123” Given string is


>>>name2.isalnum() alpha numeric
True
>>>name1=“123computer“
>>>name1.isalnum()
True
>>>name3=“Praveen”
>>>name3.isalnum()
True
22. islower() Method

islower() method is used check string


contains all lower case letters or not, it returns
true or false result.
Given string is not
>>>name2=“Praveen” lower case string
>>>name2.islower()
False
>>>name1=“praveen“
>>>name1.islower()
True Given string is
lower case string
23. isupper() Method

isupper() method is used check string


contains all letters upper case or not, it returns
true or false result.
Given string is not
>>>name2=“Praveen” upper case string
>>>name2.isupper()
False
>>>name1=“PRAVEEN“
>>>name1.isupper()
True Given string is
upper case string
24. isspace() Method

isspace() method is used check string


contains space only or not.

>>>name2=“ ” Given string


>>>name2.isspace() contains space only
True
>>>name1=“PRAVEEN M J“
>>>name1.isspace()
False
Given string not
containing space only
25. find() Methods

find() method is used to find a


particular string (substring) in a given
string.

>>>name=“Classic” the first position


>>>name.find(“s”) where “s” appears
3

String object String Method Method Argument


26. str() Method

str() method is used convert non


string data into string type.

>>>str(576)
‘576’

576 is number converted to string


27 len() Method

len() method is used get a length of


string.

>>>len(“Praveen”)
7

Gives the string length


28 max() Method

max() method is used get a max


alphabet of string as per ASCII values.

>>>max(“Praveen”)
v

Gives max character


29 min() Method

min() method is used get a max


alphabet of string.

>>>min(“Praveen”)
P

Gives min character P because it has


ASCII Value 65
30 split() Method

split() method is used split a string.

Split in to several words or substrings


31 index() Method

Same as find(), but raises an


exception if str not found.
>>> name="Sainik“
>>> name.index("a",3,5)
ValueError: substring not found
>>> name.index("a",1,5)
1 Value error

Character found, returning the position


Character Methods
32. ord() Method

ord() method is used get a ASCII


value for a character.

>>ord(“a”)
97

97 is the ASCII value for character ‘a’


33. chr() Method

char() method is used get a


character for an ASCII value.

>>chr(97)
‘a’

‘a’ ASCII value is 97


Thank You

You might also like