Day 6: Strings in Python

>>> website = "https://soumilroy.com is      my site"
>>> website
'https://soumilroy.com is      my site'
>>> message = "I'm a programmer"
>>> message
"I'm a programmer"
>>> message2 = 'I\'m a programmer'
>>> message2
"I'm a programmer"

String operators

# Concatenation
>>> "Darth" + " " + "Vader"
'Darth Vader'

# Multiplication
>>> meme = "lo"
>>> meme * 6
'lolololololo'
>>> meme + 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

Strings are indexed. Only works with string. For example

>>> name = "Soumil"
>>> name[0]
'S'
>>> name[1]
'o'
>>> name[2]
'u'
>>> name[3]
'm'
>>> name[4]
'i'
>>> name[5]
'l'
>>> name[0] + name[3]
'Sm'
# Get characters from EOS
>>> name[-1]
'l'
>>> name[-2]
'i'
# Index that doesn't exist
>>> name[10]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

“None” type

Similar to null in javascript, python has its own type to declare non-values. Its called none. Its not 0 or empty string ""

>>> user = None
>>> user
>>> type(user)
<class 'NoneType'>

Slicing strings

>>> message = "Hello Python!"
>>> message[6:15] # from 6th character till the 15th
'Python!'

Some more examples

>>> message[6:-1]
'Python' # -1 being EOS
>>> message[-15:-8] # Slicing from EOS
'Hello'
>>> message[4:] # from index 4 till the end
'o Python!'
>>> message[:6] # from index 0 till 6
'Hello '

Slicing with steps

>>> message[2:10:3] # 3 being the skip step
'l t'
>>> message[2:15:1] # 1 default
'llo Python!'
>>> message[2:15:2] # skip every 2 chars
'loPto!'

Escape characters

# Escape characters
# Examples: \n, \t, \b, \r, \v, \", \', \\

print ("Hello \nPython!") # New line
print ("Hello \t\tPython!") # Tab
print ("Hello \bPython!") # Backspace
print ("Hello \rPython!") # Carriage return
print ("Hello \vPython!") # Vertical tab
print ("\"Hello Python!\"") # Double quotes
print ("Hello \\Python!") # Backslash

Multi-line strings

message = """
	A quick brown fox 
	jumps over the lazy dog.
	Lorem ipsum dolor sit amet, 
	consectetur adipiscing elit.
"""
print (message) # preserves newlines & tabs

Side note: On my last job, I faced this issue when sending multiline text through GraphQL, the backend returned error because I sent it with double quotes (""). The solution was simply wrapping the text with triple quotes

Basically, python converts triple quotes text into regular text with escape characters.

'\n\tA quick brown fox \n\tjumps over the lazy dog.\n\tLorem ipsum dolor sit amet, \n\tconsectetur adipiscing elit.\n'

Built-in string functions

# String length function
print (len(message)) # 107
print (len(107))
TypeError: object of type 'int' has no len()

The Input function

The input prompts the user for some input and converts it to a string and then returns it. The input function is not available from python runtime, so it must be executed from a python file.

name = input ("Enter your name: ")
print ("Welcome to the matrix, " + name + "!")

age = input ("Enter your age: ")
print ("You are " + age + " years old.")
What’s your Reaction?
+1
0
+1
0
+1
0

Published by Soumil Roy

I'm a Full-Stack Developer with 7+ years of experience in Web design & development .Currently building expertise in React, TypeScript & GraphQL

Leave a comment

Your email address will not be published.

Open WhatApp
Hello 👋 Click the button below connect with me.