Python String Methods, String Slicing and Exercises

Get the Employee Details from the Database for the Employee who has logged in using Sessions

 
      Accessing a Character from a String

  Characters in a string can be accessed using the standard [ ] 

  k="test"
  print(k[0])
  print(k[1])
  print(k[2])
  print(k[3])

    
Output t e s t

Python allows negative indexing for its sequences

 
      k="test"
      print(k[-1])
      print(k[-2])
      print(k[-3])
      print(k[-4])

    
Output t s e t

String Concatenation

 
      k="python "
j="programming "
l="for "
m="beginners"
print(k+j+l+m)

    
Output python programming for beginners

String Repetition

 
      k="test"
print(k*3)
    
Output testtesttest

String Membership Test

 
      k="test"
print("t" in k)
Output
True
k="test this"
print("this" in k)
    
Output True

Membership Test Exercise 2

 
      k="test this"
print("thus" not in k)
    
Output True

String Slicing

 
      k="testing"
print(k[1:])
print(k[1:4])
print(k[:])
print(k[0:100])
    
Output esting est testing testing

String Slices - Negative Indexing

 
      k="testing"
      print(k[:-1])
      print(k[-3:])
      print(k[-5:6])
    
Output testin ing stin