Python Fundamentals

Python String

What Is A String In Python it’s a sequence of uni-code character and one of the most popular Python Variable, we are going to explain how to deal with it

What is String in Python

python string

String is a sequence of character coded into quotes ‘ , ” , ‘ ‘ ‘ , “”” .

say_hi = 'hi'

say_hi2 = "hi"

say_hi_to_world = ''' hi mom, hi dad
                      thanks for bringing me to the world'''

say_hi_to_someone = """hi lural
                       you're a good friend"""

What Is A String In Python Element(s)

python auto index characters in the string , elements of a string cannot be changed or deleted

What Is A String In Python Index

python has a clear indexing systems that uses only integers and starts from 0 to as many characters as the list have

String with 5 characters : char number one index is 0 and number 2 is 1 and so on to 4

string1 = 'Python Monsters'
print('string1 = ', string1)

print('string1[0] = ', string1[0])
# first character

print('string1[-1] = ', string1[-1])
# last character

print('string1[1:5] = ', string1[1:5])
# slicing 2nd to 5th character

print('string1[5:-2] = ', string1[5:-2])
# slicing 6th to 2nd last character

String Common Escape Sequences

  • \\     : Backslash
  • \’     : Single Quote
  • \”    : Double Quote
  • \n   : New Line
  • \t    : Tab
  • \v   : Vertical Tab

What Is A String In Python Special Operators

  • +    : Adds values on either side of the operator
  • *    : a = ‘cat’ >>>  a*2  >>> catcat
  • []   : Slice
  • [ : ]: Range Slice
  • in / not  in : Membership check
  • % : format < Explained Next >

What Is A String In Python Format

print("{}, {} and {}".format('John','Sozi','Sean'))

String Common  Methods

  • format()     : Previously explained
  • lower()       : lower-case characters
  • upper()      : upper-case  characters
  • join()          : Merge sequences
  • split()         : split words to list
  • find()         : what index ?
  • replace()   : replace characters

Write a Comment

Your email address will not be published. Required fields are marked *