Data Type Of Python
In the last session we discus about what is the data type of python. And we see List data type. In this session we see the next data type String.
2) String- String is a ordered collection of immutable data type. String are iterable and hashable. Strings are always declared in single or double quotes. - Single line string:-
S = "Python" - Multi line string:-
S = """ Line 1
Line 2
Line 3 """ - Iterable:-
S= "Program"
for i in S:
print(i)
Output:-
P
r
o
g
r
a
m - Membership:-
print( 'p' in S) => True
print( 'P' not in S) => False
- capitalize() :- It return the capitalize version of the string. More specifically it make the first character have uppercase and rest lowercase.
For example:-
S = "hello World"
print(S.capitalize()) => "Hello world" - count(substring, start,end):- It returns the number of non-overlapping occurrences of given substring in a string with slice notation start and end.
For example:-
S = "Hello world"
print(S.count('l')) =>3
In the given string the substring l occurs 3 times.
print(S.count('l',0,5)) => 2
In the above example substring is occur 2 times between index number 0 to index number 5. - endswith(substring,start,end):- It returns true if string end with the specified suffix otherwise it returns flase.
For example:-
S = "Hello world"
print(S.endswith('d')) => True
In the above example it returns true because string S is end with d.
print(S.endswith('r')) => False
In the above example it returns false because string S is not end with r.
print(S.endswith('lo',0,5)) => True
In the above example it returns true because in index number 0 to index number 5 it end with lo. - startswith(substring,start,end):- It returns true if string start with specified suffix otherwise it returns false.
For example:-
print(S.startswith('H')) => True
print(S.startswith('w',6)) => True
print(S.startswith('w',5)) => False - find(substring,start,end):- It returns the lowest index in a string where substring is found. It returns -1 if substring is not found.
For example:-
S = "Hello world"
print(s.find('l')) => 2
In the above example l come three times but find method returns only lowest index number so it return 2.
print(S.find('l',3)) => 3
print(S.find('l',10)) => -1 - index(substring,start,end):- It is similar as find but difference is, it raises value error when substring is not found.
For example:-
print(S.index('l',10)) => ValueError - lower():- It returns copy of string converted to lowercase.
For example:-
S = "HelLo wOrld"
print(S.lower()) => "hello world" - upper():- It returns copy of string converted to uppercase.
For example:-
S = "hello world"
S.upper() => "HELLO WORLD" - swapcase():- It invert the case of character that means it convert lowercase character to uppercase and vice versa.
For example:-
S = "HelLo wOrLd"
S.swapcase() => "hELlO WoRlD" - title():- It return version of string where each word is a title case. most specifically words starts with uppercase character and all remaining cased character have lower case.
For example:-
S = "The quick brown fox"
print(S.title()) => "The Quick Brown Fox" - isalnum():- It returns true if the string is alpha numeric string otherwise it returns false.
For example:-
S="admin1234"
print(S.isalnum()) => True - isalpha():- It returns true if the string is alphabetic string otherwise false.
For example:-
S="admin1234"
print(S.isaplha()) => False
S1 = "admin"
print(S1.isalpha()) => True - isdigit():- It returns true if the string is digit string otherwise false.
For example:-
S="admin1234"
print(S.isdigit()) => False
S1 = "admin"
print(S1.isdigit()) => False
S2 = "1234"
print(S2.isdigit()) => True - islower():- It returns true if the string is the lowercase string otherwise it returns false.
For example:-
S="admin1234"
print(S.islower()) => True
S1 = "ADMIN"
print(S1.islower()) => False - isupper():- It returns true if the string is the uppercase otherwise it returns false.
For example:-
S="ADMIN"
print(S.isupper()) => True
S1="Admin"
print(S1.isupper()) => False - istitle():- It returns true if the string in the title format otherwise it returns false.
For example:-
S="the quick brown fox"
print(S.istitle()) => False
S1 = "The Quick Brown Fox"
print(S.istitle()) => True - isspace():- It returns true if the string is a white space string, otherwise it returns false.
For example:-
S =" "
print(S.isspace()) => True
S1 =" Hello"
print(S1.isspace()) => False - lstrip(chars=none):- It returns the copy of the string with leading white space remove. If chars is given and it is not a null it remove character in a string.
For example:-
S = " Hello"
print(S.lstrip()) => "Hello"
S1="aaa@bbbccaHello"
print(S1.lstrip('a@bc')) => "Hello"
In this example it removes a@bc characters from the string. It doesn't matter how many that
characters are present in the string. - rstrip(chars=none):- It return the copy of the string with trailing whitespaces.
For example:-
S="Hello "
print(S.rstrip()) => "Hello" - strip(chars=none):- It returns copy if string leading as well as trailing white spaces removes.
For example:
S = " Hello "
print(S.strip()) => "Hello" - partition(separator):- It partition the string into three parts using the given separator. This will search for the separator in the string. If the separator is found it returns three tuples containing the part before the separator, the separator itself, and the part after it. If the separator is not found it return a three tuple containing the original string and two empty string.
For example:-
S="The quick brown fox jumps"
print(S.partition(' ')) => ( 'The' , ' ' , 'quick brown fox jumps' )
In above example we give separator as a one white space so it return the tuple containing the part before separator, separator itself and part after separator. - replace(old,new,count=-1):- It return a copy with all occurrences of substring old replace by new. The count is maximum number of occurrences to replace. -1 means replace all occurrences.
For example:-
S = "Hello World"
print(S.replace('l','L')) => "HeLLo WorLd"
print(S,replace('l', 'L', 2)) => "HeLLo World" - split(sep=none,maxsplit=-1):- It return a list of the words in the string using separator.
For example:-
S = "The quick brown fox jumps"
print(S.split()) => [ 'The', 'quick', 'brown', 'fox', 'jumps' ]
print(S.split(maxsplit=3)) => [ 'The', 'quick', 'brown', 'fox jumps' ] - join(iterable):- It concatenate any number of string from the list.
For example:-
L = [ 'The', 'quick', 'brown', 'fox', 'jumps' ]
print(' '.join(L)) => "The quick brown fox jumps"
L1 = [ 'pq', 'rs', 'xy' ]
print('#'.join(L2)) => 'pq#rs#xy'
Comments
Post a Comment