Skip to main content

Python | Data Structure or Data Types (String)

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

Methods of string:-
  1. 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"

  2. 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.

  3. 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.

  4. 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

  5. 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

  6. 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

  7. lower():- It returns copy of string converted to lowercase.
    For example:-
    S = "HelLo wOrld"
    print(S.lower()) => "hello world"

  8. upper():- It returns copy of string converted to uppercase.
    For example:-
    S = "hello world"
    S.upper() => "HELLO WORLD"

  9. 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" 

  10. 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"

  11. isalnum():- It returns true if the string is alpha numeric string otherwise it returns false.
    For example:-
    S="admin1234"
    print(S.isalnum()) => True

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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. 

  19. rstrip(chars=none):- It return the copy of the string with trailing whitespaces.
    For example:-
    S="Hello        "
    print(S.rstrip()) => "Hello"

  20. strip(chars=none):- It returns copy if string leading as well as trailing white spaces removes.
    For example:
    S = "           Hello           "
    print(S.strip()) => "Hello"

  21. 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.

  22.  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"

  23. 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' ]

  24. 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'

This all are the methods of string. In the next section we will see the set data type and their methods.

Comments

Popular posts from this blog

Basic Knowledge of Python part 1

Basic Mathematics equations using Python- As we know python is a simple and easy to learn programming language. In this section we learn how to done mathematics equations using python. To perform this we use our basic operators +,-,/,* .  To perform python we have to open python interpreter. In this interpreter you can type simple equation like 5+5, 4/2 etc. For example: The integer number (e.g. 1,2,3....) have type int and the number with fractional part (e.g. 2.5,2.1,4.5...) have type float. Note:-  The division type always returns float. To get only an integer result of division you can use the // operator and to get remainder from division result you can use % operator. In python you can easily calculate power of the number by using ** operator. Like this you can easily calculate mathematics equation. Now we see how to find area of rectangle. Area = length*width is the formula to find area of rectangle. Here '=' sign is used to store value to the variable. You can als...

Python | Data Structure Or Data Type (Tuple)

Data Type Of Python  In this session we will see last data type of python which is Tuple. 5) Tuple:- Tuple is a ordered collection of immutable datatype, tuples are quite similar to lists but difference is list are mutable where as tuple are immutable. Empty tuple:-  t = () or tuple() Tuple with single element:- t = (10,) Tuple with multiple element:- t1 = ( 10, 20, 30, 'Hello' ) Nested tuple:-  t1 = ( 10, 20, 30, 'Hello' ) p = t2, 30, 40, 50 print(p) => ((10, 20, 30, 'Hello'), 30, 40, 50) Methods of Tuple:-   count(value):- It return the count of value if the value is present in the tuple else it return 0. For example:- t = (10, 20, 30, 'Hello') t.count(10) => 1  t.count(50) => 0 index(value,start,end):- It return the index number if the value is present in the tuple, otherwise it raised value error. For example:- t = (10, 20, 30, 'Hello') t.index(10) => 0 It return the index number of value 10. Prev Topic   Next Topic