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

Python | Introduction

Python is developed by Guido van Rossum . Guido van Rossum started implementing python in 1989. Python is very simple programming language so if you are new to programming, you can learn python without facing any issue. Features of Python Free and Open Source -                                                                                      Python is a open source language. We can easily install python.It is freely available at python's official website . High level language -                                                                                     ...

Python | Recursive Function

Python - Recursive Function Recursive function means the function call themselves within the function body until a certain condition is matched then this function is recursive function. For example:-  def fact(n):      if n==0 or n==1:           return 1      else:           return n*fact(n-1) print(fact(8)) Output:- 40320 This is the example of recursive function. In this program we create a function named as fact and we call this function within the same function. How this program works? We give the value of n is 8 so n=8. In the first if condition, 8 is not equal to 0 and 1 so it goes to the else part in the else part the sentence is return n*fact(n-1) that means 8*fact(7) now here value of n become 7 to find fact(7) again same procedure repeat up to fact(1).   Because fact(1)=1   we give this condition. Then  fact(2) => 2*fact(1) =>  2*1 =>  2   ...

Python | Control Flow Tools

Control Flow Tools In this session we will see about control flow tools of python. 1) if Statement:- In the programming language if statement is important statement. Now we see how to write if statement in the python program. For Example:- >>> x = int(input("Please Enter an integer")) Please Enter an integer 50 >>> if x<0:      x=0      print("Negative change to zero") elif x==0: print("Zero") elif x==1: print("One") else: print("More") More >>>  Here is the program of if statement. You can add one or more elif part in the program. else part is the optional.      In the program we take the value of x is 50. In the if statement we write x<0 condition which is false because 50 is not less than 0 so it will go to the next part of the code which is first elif part, here we give condition x==0 this condition is also false so it will go to the second elif part which is x==1 this condition is also f...