#5 Python :- String

 Strings:- 

                              Collection of characters inside single quotes or double quotes.

In python strings are writing with quotation of single or double commas.

Let us look at examples which of the followings are strings or not:-

Q:- a) a="Gurpreet Singh"   ----> String

      b) a='Gurpreet Singh'   ----> String

      c) a= "I'm a string"       ----> String

      d) a='I'm a string'         ----> Not a String

      e) a='I\'m a string'       ----> String

Now we learned to find either given variable is String or not.

 Properties of String:- 

           i) Immutable (it means we can't change the values of string)

                Code:-

            Q:-  mystring= "Monu"

              #convert monu into Sonu

               mystring["0"]=S

               print(mystring)  -----> Error (Since strings are immutable so we can not change)

             #  So if you want to convert "Monu" to "Sonu". Simply right the below code

                print('S'+mystring[1:]) ---> Sonu

            Q:  If mystring="ring"  then convert it  into finger.

              Code:-             mystringh="ring"

                                    print('f'+mystring[1:]+'er') ---> finger 

                  

           ii) Ordered - Which means that we can do slicing and indexing.

             a) Indexing of strings:- It means to picking up single word from the string.

               e.q. :- mystring ="Gurpreet Singh" 

                   # indexes of given string, G=0; u=1; r=2; p=3; r=4; e=5; e=6; t=7,  blank space=8; S=9;

                  # i=10; n=11; g=12; h=13.

               print(mystring[0]) ----> G

               print(mystring[2]) ------> r

               print(len(mystrings))   ------> 14 #(Syntax len() is used to know the length of the given string)

              print(mystring[14])  ------> Error (since the index number of last digit is 13 after that all numbers it shows the error).

         Note:- We can also do reverse indexing which is in negative numbers.

             b) Slicing of given string:- It means to picking a group of words from given strings.

                         Syntax for Slicing [start:stop(not include):step]

                  e.q.   mystring="Gurpreet Singh"

                      print(mystring([1:5]) ---> urpr 

 

          iii) String Interpolation:-

               a) .format method:-

                                       To adding something in the string.

                  e.q. Input:- print("My name is {}" .format('Gurpreet Singh'))

                      Output:- My name is Gurpreet Singh

                      Input:- print("My {} {} {}" .format("Gurpreet Singh", "is", "name"))

                      Output:- My Gurpreet Singh is name

                      Input:- print("My {2} {1} {0}" .format("Gurpreet Singh", "is", "name"))

                      Output:- My name is Gurpreet Singh

                       Input:- print("My {c} {b} {a}" .format(a="Gurpreet Singh",  b="is",  c="name"))

                      Output:- My name is Gurpreet Singh


             a) .f-string method:-

                      Input:- a="Gurpreet Singh"

                                   print(f"My name is {a}")

                      Output:- My name is Gurpreet Singh

                    Input:- a="Gurpreet Singh"

                                b="name"

                               print(f"My {b} is {a}")

                      Output:- My name is Gurpreet Singh


            iv)Strip methods:- 

                     It is used to remove the extra spaces in strings

           e.q.             mystring= "      Gurpreet      "

                   dots= ".........."

           print(mystring + dots) 

           print(mystring.lstrip() + dots) 

           print(mystring.rstrip() + dots) 

           print(mystring.strip() + dots) 

                 Gurpreet       ..........

          Gurpreet       ..........

                 Gurpreet .......... 

          Gurpreet ..........

      Note:- Strip method never remove spaces in between the strings. To do so we have to use .replace()

                method.

        e.q.  e.q.:- mystring="Gur      preet"

                        print(mystring.strip)  --------> Gur      preet

                        print(mystring.replace(" ", ""))  --------> Gurpreet

      v) Some imp methods:--

             a) .lower() method:- used to make all characters in string small.

             b) .upper() method:- used to make all characters in capital letters.

            c) .title() method :-  used to write first letter in capital and other letters in small alphabets.

            d) .count() method :- used to count the no of times the same character comes.

            e) .replace() method :- used to replace one character with other character. #(old, new, count)

            f) .find() method :- used to find the strat position of character in a string.#(sub, start, end)

           g) .center() method:- used to add any character on both side of a string. #(width, "character")

           e.q.:- name= input("Enter Your name:- ") 

                    print(name.center(len(name)+4, "*"))

            Output:- Enter Your name:- Gurpreet Singh 

                          **Gurpreet Singh**


          Small project

         Code:- 

               a= input("Add this number ",) 

               b= input("with ") 

               print(f"gives {int(a)+int(b)}")

          Output:-

                  Add this number 55

                  with 65

                 gives 120

Code:- Take 2 input in one line

               name, age= input("Enter your name and age ")  .split()

               print(name)

               print(age)

          Output:-

                  Enter your name and age Gurpreet 21 #(Space is must between two varriables)

                  Gurpreet

                  21

Code:- Take 2 input in one line seperated by comma

              a) First input is name and calculate its lenght without spaces .

              b) Second input is a character and count no of characters in case incensitive.

               name, char= input("Enter your name and character:- ")  .split(",")

               print(len(name.strip()))

               print(name.strip().lower().count(char.strip().lower()))

          Output:-

                  Enter your name and character:-     Gurpreet Singh    ,      G

                  14

                  2




              

Comments

Popular posts from this blog

#4 Python :- Variables

#3 Python :- Comments, Printing First Statement and Escape sequences:-