AI Basics with AK

Season 02 - Introduction to Python Programming

Arun Koundinya Parasa

Episode 05

Strings & Characters - Part2

Agenda

  • Strings { Last Episode }
  • String Mathematical Operations { Last Episode }
  • Indexing { Last Episode }
  • Slicing { Last Episode }
  • String Operations
  • New Style Operators { Next Episode }

String Operations

var = "It is a very beautiful day!"
print(var)
splitvariable = var.split()
print("\n",splitvariable)
print("\n",len(splitvariable))
It is a very beautiful day!

 ['It', 'is', 'a', 'very', 'beautiful', 'day!']

 6
print(var.split('d'))
print("\n",len(var.split('d')))
['It is a very beautiful ', 'ay!']

 2
print(var.split('!'))
print("\n",len(var.split('!')))
['It is a very beautiful day', '']

 2
var = "I have 10$ cash with me!"
print(var)
splitvariable = var.split('$')
print("\n",splitvariable)
print("\n",len(splitvariable))
I have 10$ cash with me!

 ['I have 10', ' cash with me!']

 2
splitvariable[0]
'I have 10'
splitvariable[0].split()
['I', 'have', '10']
splitvariable[0].split()[2]
'10'
print(splitvariable)

''.join(splitvariable)
['I have 10', ' cash with me!']
'I have 10 cash with me!'
'$'.join(splitvariable)
'I have 10$ cash with me!'
var = ["I","want","to","dance","now!"]

' '.join(var)
'I want to dance now!'
'space'.join(var)
'Ispacewantspacetospacedancespacenow!'
join_seperator = " %& "
var = ["This","is","just","an","Example"]
print(join_seperator.join(var))
print(" ")
print(type(join_seperator.join(var)))
This %& is %& just %& an %& Example
 
<class 'str'>
print(type(var))
<class 'list'>

this is list variable type which we wil discuss in future episodes.

String Operations

Strip

new_var = "  Wow! there is a tree!! over there! "
print(new_var)
  Wow! there is a tree!! over there! 
new_var.strip()
'Wow! there is a tree!! over there!'
new_var.lstrip()
'Wow! there is a tree!! over there! '
new_var.rstrip()
'  Wow! there is a tree!! over there!'
new_var2 = "What the program \\ "
print(new_var2)
What the program \ 
new_var2.strip(' \\ ')
'What the program'
new_var3 = "Hi!!! I Miss you a lot ..:)"
new_var3.strip(':().!')
'Hi!!! I Miss you a lot '
new_var3.strip(':().!' + ' ')
'Hi!!! I Miss you a lot'

String Operations

var = '''You know my name
You know my name
You know my name
You know my name
You know my name
You know my name
'''
var.find('You')
0
var.rfind('You')
85
var.find('you')
-1
var.startswith('You')
True
var.endswith('name')
False
var.endswith('\n')
True
var
'You know my name\nYou know my name\nYou know my name\nYou know my name\nYou know my name\nYou know my name\n'
var.count('You know my name')
6

Replace

new_var = var.replace('You','I')

print(new_var)
I know my name
I know my name
I know my name
I know my name
I know my name
I know my name
print(var.replace('You','I',3))
I know my name
I know my name
I know my name
You know my name
You know my name
You know my name

CaseStatement

var = "i am a spiritual cognitive scientist."
var.capitalize()
'I am a spiritual cognitive scientist.'
print( var.upper() )
print(" ")
print(var.lower())
I AM A SPIRITUAL COGNITIVE SCIENTIST.
 
i am a spiritual cognitive scientist.
print(var.title())
print("")
print(var.title().swapcase())
I Am A Spiritual Cognitive Scientist.

i aM a sPIRITUAL cOGNITIVE sCIENTIST.

Thank You

Open In Colab