JB Institute ofEngineering and technology
Name:k.Indhu
Branch: CSM-B
ROLLNO:24671A6681
Topic: strings and it’s functions and methods
2.
AboutStrings
Strings area sequence of unicode characters.A string is
a text.
In python,strings are immutable and ordered.
Strings is one of the standard data type in python.
Creating a strings:
There are different ways to create strings in python like in
single, double,as well as in triple quotes.while creating a
strings if a string has any type of quotes then in declaration
you should use a different type of quote to avoid an error.
3.
Indexing
Examples:
S=‘Hello’#single quotes
S=‘It’s rainingoutside ‘#(syntax error)
S=“It’s raining outside”#correct way
S=’’’Good morning ’’’# Three single quotes
Indexing:
Indexing is the same in a string as in an array means zero-
based indexing.
In simple words,the zero indexes represents the first
character in a string and as the index increases,it moves
forward.
In python,indexing is classified as positive and negative
indexing.
4.
Positiveand
Negative
indexing
1.Positive indexing:
Positive indexingis the same as we study zero-based indexing
means moving from zero to the end length of a string.
S=“Hello”
Print (s[0])#displays H
2.Negative indexing :
Negative indexing means accessing the elements of string from
backward.If we want to access the last character so string in
positive indexing it is a long method as we have to find the length
of the string and subtract one from it instead we can directly use
the negative one in this case.
Example:s=“Hello”
print(s[-1])#displays o{H e l l o}
. {-5-4-3 -2-1}
5.
Slicing
3.Slicing:
When wehave long strings like sentences or paragraphs
and want to access some parts like complete words then
we cannot use only indexing so here lies the concept of
slicing which is achieved using the colon operator.
In the square bracket,we need to define starting index and
ending index separated witha a colon.ao the output will be
part of the string from a start index to end index minus
one.
Example:”Hello world”
Print(s[1:4])#displays ell
In the above example start from one index till the third index.
Print(s[0:8:3])#display hlw
6.
Operations on
python string:
1.Stringaddition (+):
+ Is concatenation operator.
The plus operator (+) is used in this respect to join.
S=“Good”+”morning”
Print(s)
O/p: Good morning
2.String mltiplication (*):
(*) The repetition operator
This operator is used to return a repeated string a specific
number of times.
S=“Good”
Print(s*3)
O/p: Good Good Good
7.
String
comparison
3.String comparison:
Itis used to compare two strings.
Returns Boolean true if two strings are same and return Boolean false if two strings
are not same in “==“operator.
Returns Boolean true if two strings are not same and return Boolean false if two
strings are same in “!=“ Operator.
Example:
String1=“Hello”
String 2=“Hello miss”
String 3=“Hello miss”
print(string1==string3)
Print(string 2==string 3)
S
Print(string 2!=string 3)
O/p: False
True
False
8.
Membership
operator
4.Membership operator:
Membershipoperator “in” and “not in”.
It is used to search whether the specific character is in a string
or not.
In this we use Boolean true and false.
Example:
String1=“Computer”
Print(“o” in string1)
Print(“z “in string1)
Print(“p “not in string1)
O/p:true
False
False
9.
StringFunctions
1.String len():Itis used to find the length of string means no.Of characters
in a string.
2.String max(): returns the largest string
String1=Monitor
String2=Keyboard
String 3=Cpu
Print(len(string1))#displays 7
Print(max())#Monitor
Print(min())#cpu
3.Title pr capitalise:
Title function capitalises the first letter of each word.
The capitalise function capitalises the first letter of a Example:
S=it’s a mango”
Print(s.capitalise())#o/p:Its A Mango
Print(s.title())#o/p: It’s a mango
10.
StringFunctions
4.Upper/Lower/Swapcase:
S=“It’s Raining Outside”
Print(s.upper())#ITSRAINING OUTSIDE
Print(s.lower())#its raining outside
Print(s.swapcase())#iTS rAINING oUTSIDE
5.Count:It counts number of any substrings are present in a string.
Example:s=“Raining”
Print(s.count(“i”))#3
6.Find/Index:
Both functions operate in exactly the same manner.
Both locate the position of a substring within a string.
S=“It’s Raining Outside”
Print(s.find(“ing”))#8
Print(s.index(“ing”))#8
Print(s.find(“down”))#-1
Print(s.index(“down”))#error
11.
Stringmethods
1.s.capitalise():This methodis used to converts the first character to
uppercase.
s.islower():It returns true ia all characters in a given string are lower
case.otherwise false.
s. lower():Used to convert all the characters ina string to lowercase.
s.isupper():It return true if all the characters in a string are
uppercase.oterwise false.
s.upper(): Used to convert all the characters in a string to uppercase.
s.isalnum():It returns true if all characters in string are
alphanumeric.otherwise false.
s.isalpha():Returns true if all characters in string are
alphabetic.otherwise false.
s.digit(): Returns true if all characters in string are digits.otherwise
false.
s.count():It is used to count the occurrence of a character or
substring in string.