Strings in Python
Strings in Python
IN
PYTHON
KISHORE P XII –
CONTENT
1. DEFINITION
2. STRING OPERATORS
3. STRING SLICING
4. STRING FUNCTIONS
5. DIFFERENCES
1. DEFINITION
STRING OPERATORS
1. CONCATENATION :
EXAMPLE :
Raises Error
2. REPLICATION :
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 :
SYNTAX :
substring = <string>[start : end]
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
1 2 3 4 5 6 7 8 9 10
c o m p _ u t e r :
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
15 16 17 18 19 20 21 22 23 24 25
r i n g a r o s e
5. index() :
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.
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.
SYNTAX :
<string>.partition(<separator/string>)
EXAMPLE :
‘working’ is the
separator string.
Find() Index()
Returns the lowest index of the Returns the lowest index of the
substring if found. substring if found.
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’