AI Basics with AK

Season 02 - Introduction to Python Programming

Arun Koundinya Parasa

Episode 04

Strings & Characters - Part1

Agenda

  • Strings
  • String Mathematical Operations
  • Indexing
  • Slicing
  • String Operations { Part2 }
  • New Style Operators { Part2 }

Strings

var = "This is Python Learning Session"
type(var)
str
var1 = 'A'
type(var1)
str
print(len(var))
print(len(var1))
31
1
var + var1
'This is Python Learning SessionA'
var - var1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-6ac0306cdaf6> in <cell line: 1>()
----> 1 var - var1

TypeError: unsupported operand type(s) for -: 'str' and 'str'
var * var1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-057e19f0f1e8> in <cell line: 1>()
----> 1 var * var1

TypeError: can't multiply sequence by non-int of type 'str'
var2 = "This is sentence one.\nThis is sentence two.\nThis is sentence three"

print(var2)
This is sentence one.
This is sentence two.
This is sentence three

Strings

var3 =  '''
This is sentence one
This is sentence two
This is sentence three
'''
print(type(var3))
print("")
print(var3)
<class 'str'>


This is sentence one
This is sentence two
This is sentence three
var3 = r'This is sentence one.\nThis is sentence two.\nThis is sentence three'
print(type(var3))
print(var3)
<class 'str'>
This is sentence one.\nThis is sentence two.\nThis is sentence three
var4 = r'''This is sentence one.
This is sentence two.
This is sentence three
'''
print(type(var4))
print("")
print(var4)
<class 'str'>

This is sentence one.
This is sentence two.
This is sentence three
var = "Julius Caesar says, "Experience is the teacher of all things.""
print(var)
  File "<ipython-input-11-d472829db0e4>", line 1
    var = "Julius Caesar says, "Experience is the teacher of all things.""
                                ^
SyntaxError: invalid syntax
var = "Julius Caesar says, \"Experience is the teacher of all things.\""
print(var)
Julius Caesar says, "Experience is the teacher of all things."
var1 = "lets print blackslash \"
print(var1)
  File "<ipython-input-13-25f80e072a07>", line 1
    var1 = "lets print blackslash \"
           ^
SyntaxError: unterminated string literal (detected at line 1)
var1 = "lets print blackslash \\"
print(var1)
lets print blackslash \
var1 = "lets print blackslash \
second part of the sentence"
print(var1)
lets print blackslash second part of the sentence

String Mathematical Operations

print(var + "\n" +var1)
Julius Caesar says, "Experience is the teacher of all things."
lets print blackslash second part of the sentence
print(var * 2)
Julius Caesar says, "Experience is the teacher of all things."Julius Caesar says, "Experience is the teacher of all things."
print(var / 2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-5d113b0a9ea4> in <cell line: 1>()
----> 1 print(var / 2)

TypeError: unsupported operand type(s) for /: 'str' and 'int'
var - "says"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-927b51de5888> in <cell line: 1>()
----> 1 var - "says"

TypeError: unsupported operand type(s) for -: 'str' and 'str'
a = " Children"
b = " Played"
c = " and"

a + b + c + b
' Children Played and Played'
"Go "*2 +"before it's late"
"Go Go before it's late"
"First Name " "& Last Name"
'First Name & Last Name'
"First Name "*2 "& Last Name"
  File "<ipython-input-42-42675f58e5c5>", line 1
    "First Name "*2 "& Last Name"
                    ^
SyntaxError: invalid syntax

Indexing

var = "WORD"
print(var)
WORD
len(var)
4
print(var[0])
print(type(var[0]))
print(var[3])
W
<class 'str'>
D
var[4]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-51-6e0795f01afe> in <cell line: 1>()
----> 1 var[4]

IndexError: string index out of range
var[-1]
'D'
var[-2]
'R'
var[-3]
'O'
var[-4]
'W'
var[-5]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-56-8ea7679abd80> in <cell line: 1>()
----> 1 var[-5]

IndexError: string index out of range

Slicing

var = "Slice and Dice Me"
print(var)
Slice and Dice Me
var[0:5]
'Slice'
var[:5]
'Slice'

[: end ] specifies from the beginning to the end offset minus 1

var[6:9]
'and'

[ start : end ] indicates from the start offset to the end offset minus

var[10:]
'Dice Me'

[ start :] specifies from the start offset to the end.

var[10:-3]
'Dice'
var[10:-10]
''
var[0:6:2]
'Sie'
var[::-1]
'eM eciD dna ecilS'
var[::-1][3:7]
'eciD'

[ start : end : step ] extracts from the start offset to the end offset minus 1, skipping characters by step

var[3:-3:2]
'c n ie'

Thank You

Open In Colab