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

Strings in Python

good

Uploaded by

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

Strings in Python

good

Uploaded by

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

STRINGS

IN

PYTHON

KISHORE P XII –
CONTENT

1. DEFINITION

2. STRING OPERATORS

3. STRING SLICING

4. STRING FUNCTIONS

5. DIFFERENCES
1. DEFINITION

• In Python, a string is a data type representing a sequence of


characters enclosed within single ('...'), double ("..."), or triple
quotes ('''...''', """...""").

• Strings are Immutable, meaning their content cannot be


modified after creation.

• They support various operations like concatenation,


slicing, formatting, and iteration, etc…
Examples :
'Hello, world!‘
"Python programming“
‘’’Computer Science’’’
2. STRING OPERATORS

STRING OPERATORS

MEMBERSHIP OPERATOR BASIC OPERATORS COMPARISON OPERATOR

IN NOT IN CONCATENATION (+) REPLICATION (*) <,<=,>,>=,==,!=


A. MEMBERSHIP OPERATOR :

• The Membership operator in string is used to check if a value exists


within a string or not.

• There are two membership operators for strings, they are:

1. IN : Returns True if the value is found in the


sequence.

2. NOT IN : Returns True if the value is not found in the


SYNTAX : sequence. EXAMPLE :
<string> in <string>
<string> not in <string>
B. BASIC OPERATOR :

1. CONCATENATION :

The operator , which is used to create new string by


joining
the two strings.
SYNTAX :
<string1> + <string2> + <string3>
+ …….

EXAMPLE :

Raises Error
2. REPLICATION :

The replication operator in strings is used to repeat a


string by a specified number of times. This operator takes one string and
one integer as operands, producing a new string that consists of the
original string repeated as many times as indicated by the integer..

SYNTAX :
<string> * <int>

EXAMPLE :
Raises error, as it is multiplied by non
integer numeral
Raises error, as it is multiplied by a
string.
C. COMPARISON
OPERATOR :

• Comparison operators are used in strings to compare values and


determine their relationship.

• They internally compare the Unicode values of the string and


return a Boolean value (true or false) based on the comparison
result.
The most common comparison operators
are :
4. Less than (<).
1. Equal to.
5. Greater than or equal to
2. Not equal to (!=). (>=).
3. Greater than (>). 6. Less than or equal to
(<=).
SYNTAX :
<string1> comparison_operator
<string2>
For ‘A’ the ASCII value is
‘65’
EXAMPLE :
‘SLICE’  ‘A PART OF’ 3. STRING SLICING

• String slicing is a technique that allows you to extract specific portions of a


string, creating a new substring from the original string.

SYNTAX :
substring = <string>[start : end]

start : The index where the slice begins (inclusive).


end : The index where the slice ends (exclusive).

EXAMPLES :
0 1 2 3 4 5 6
A M A Z I N G

-7 -6 -5 -4 -3 -2 -1
OUTPUT :
0 1 2 3 4 5 6
A M A Z I N G

-7 -6 -5 -4 -3 -2 -1

It reverses the
string

Takes every 2nd character


Ending index
Starting index
4. STRING FUNCTIONS

1. len() 9. islower() 17.


istitle()

2. Capitalize() 10. isspace() 18.


replace()

3. Count() 11. isupper() 19.


join()

4. Find() 12. lower() 20.


split()

5. Index() 13. upper() 21.


partition()

6. Isalnum() 14. lstrip(), rstrip(),strip()

7. Isaplha() 15. startswith(), endswith()


1. len() :
It returns the total count of the characters in the
passed
string. 1 2 3 4 5 6 7 8
SYNTAX : c o m p u t e r
len(<string>)

EXAMPLE : Length of the above string is ‘ 8 ’

1 2 3 4 5 6 7 8 9 10
c o m p _ u t e r :

Length of the above string is ‘ 10 ’


2. capitalize() :
It returns the copy of the string with its first
character
capitalized.
SYNTAX :
<string>.capitalize()

EXAMPLE :
3. count() :
It returns the number of occurrences of the substring
sub in
string.

SYNTAX :
<string>.count(sub[,start[,end]])

EXAMPLE :

Starting index

Ending index
It looks for ‘h’ from index ‘0’
to ‘7’
It looks for ‘h’ from index ‘0’
to ‘4’
4. find() :
> It returns the lowest index in the string where the
substring
sub is found within the slice range of start and end.
NOTE : It return the index of 1st
> returns -1 , if the sub is not found
occurrences only.

SYNTAX :
<string>.find(sub[,start[,end]])
Sub is found in ‘0’ index and ‘6’
EXAMPLE : index. As it takes only 1st
occurrence, the output is ‘0’.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
i t g o e s - r i n g a r i n g a r o s e s

From ‘0’ to ‘14’ means, it will take upto ‘13’


only, so there is no sub in that range, so it
returns ‘-1’

At ‘16’ the sub is found.

15 16 17 18 19 20 21 22 23 24 25
r i n g a r o s e
5. index() :

• It is same as the find() function.


• It returns the lowest index where the specified
substring if found.
• It raises an exception [error] when the substring is
not found in the string.
The only difference between find() and index()
function.
SYNTAX :
<string>.index(sub[,start[,end]]) As the substring is not found, it
raises an error
EXAMPLE :
6. isalnum() :
It returns True if the characters in the string are alphanumeric
(alphabets
and numbers) and there is at least one character ,otherwise it
returns
False.

NOTE : Space (‘ ‘) is not treated as alphanumeric

SYNTAX : <string>.isalnum()

EXAMPLE :
Since it contains space, it returns
FALSE
7. isalpha() :
It returns True if all the characters in the string are alphabets and
there is at
least one character, otherwise it returns False.

SYNTAX :
<string>.isalpha()

EXAMPLE :
8. isdigit() :
It returns True if all the character in the string are digits. There
must be at
least one character, otherwise it returns False.

SYNTAX :
<string>.isdigit()

EXAMPLE :
9. islower() :
It returns True if all cased characters in the strings are
Lowercase. There must be at least one cased character. It returns False
otherwise.

SYNTAX :
<string>.islower()

EXAMPLE :
10 . Isspace() :
It returns True if there are only whitespace
characters in the string. There must be at least one character.
Otherwise it returns False.

SYNTAX : <string>.isspace()

EXAMPLE :

No whitespace
11.isupper() :
It tests whether all cased characters in the string
are uppercase and requires that there be at least one cased
character. Returns True if so, otherwise returns False.

SYNTAX :
<string>.isupper()

EXAMPLE :
12. lower() :
It returns a copy of the string converted to lowercase.

SYNTAX :
<string>.lower()

EXAMPLE :
13. upper() :
It returns a copy of the string converted to
uppercase.

SYNATX :
<string>.upper()

EXAMPLE :
14. lstrip(), rstrip(), strip() :

• lstrip() : Returns the copy of the string with leading whitespaces removed.
ie. The whitespaces from the leftmost end are removed.
SYNTAX:
<string>.lstrip()

EXAMPLE :

• rstrip() : Returns the copy of the string with trailing whitespaces removed.
ie. The whitespaces from the rightmost end are removed.
SYNTAX:
<string>.rstrip()

EXAMPLE :
• strip() :

> Returns the copy of the string with leading and trailing
whitespaces removed.
> ie. The whitespaces from the leftmost and rightmost
ends are
removed.
SYNTAX :
<string>.lstrip()
EXAMPLES :
15. startswith(), endswith() :

• startswith() : Returns True if the string starts with the substring sub.
Otherwise, it
returns False.
EXAMPLE :

SYNTAX :
<string>.startswith()

• endswith() : Returns True if the string ends with the substring sub. Otherwise,
it
returns False.

SYNTAX : EXAMPLE :
<string>.endswith()
16. title() :
It returns a title-cased version of the string where all
words
start with uppercase characters and all remaining
letters are in
lowercase.

SYNTAX :
<string>.title()

EXAMPLE :
17. istitle() :
It returns True if the string has the title case, (ie., the
first letter of each word in uppercase, while rest of the letters in
lowercase),false otherwise.

SYNTAX :
<string>.istitle()

EXAMPLE :
18. replace() :
It returns a copy of the string with all occurrences of
substring old replaced by new string.

ie. It returns the copy of the string by replacing the old


string with the new sub string.

SYNTAX :
<string>.replace(old,new)

EXAMPLE :
19. join() :
It joins a string or character after each member of the
string iterator. ie.,a string based sequence.

SYNTAX :
<string>.join(<string iterable>)

1. If the string based iterator is a string, then the <str> is inserted after
every character of the string.

EXAMPLE :
2. If the string based iterator is a list or tuple of strings then, the given
string/character is joined with each member of the list or tuple, but the
tuple or list
must have all member as strings otherwise python will raise Error.

EXAMPLE :

As it is not a string,
it raises an Error
20. split() :
It splits a string based on given string or character and
returns
a list containing split strings as members.
SYNTAX :
<string>.split(<string/char>)

1. If u do not provide any argument to split, then by default it will split the
given string considering whitespaces as a separator.

EXAMPLE :
2. If you provide a string or a character as an argument to split(), then the
given string is divided into parts considering the given string/character as
separator and separator character is not included in the split strings.

EXAMPLE :
The given string is divided
from positions containing ‘o’
18. partition() :
The partition method splits the string at the first
occurrence of
separator, And returns a tuple containing three items.

1. The part before the separator.


2. The separator itself.
3. The part after the separator.

SYNTAX :
<string>.partition(<separator/string>)

EXAMPLE :
‘working’ is the
separator string.

It returns a tuple with 3


elements
5. DIFFERENCES

A. DIFFERENCE BETWEEN find() AND index() :

Find() Index()

Returns the lowest index of the Returns the lowest index of the
substring if found. substring if found.

Returns -1 if the substring is not It raises value error when the


found. substring is not found.

Does not raise an error if the Raises Error.


substring is not found.
A. DIFFERENCE BETWEEN split() AND partition() :

Split() Partition()

It will split the string at any occurrence It will only split the string at the first
of the given argument. occurrence of the given argument.

It will return a list type containing the It will return a tuple type containing the
split substrings. split substrings.

The length of the list is equal to the It will always return a tuple of length 3,
number of the words, if split on with the given separator as the middle
whitespaces. value of the tuple.
KISHORE P XII – ‘C’

You might also like