Thulasi's Thoughts -
Menu
Skip to content

Category Archives: CodeNotes

Python Basics

4 / 24 / 20

Strings

String literals in python are surrounded by either single quotation marks, or double quotation marks.

‘hello’ is the same as “hello”.

You can assign a multiline string to a variable by using three quotes with either single or double quotation marks:

a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""

Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters.

However, Python does not have a character data type, a single character is simply a string with a length of 1.

Square brackets can be used to access elements of the string.

a = "Hello, World!"
print(a[1]) # e

Slicing

You can return a range of characters by using the slice syntax.

Specify the start index and the end index, separated by a colon, to return a part of the string.

Get the characters from position 2 to position 5 (not included):

b = "Hello, World!"
print(b[2:5]) # llo

String Length

The len() function returns the length of a string:

a = "Hello, World!"
print(len(a)) # 13

About Me

Hello! I'm Thulazi (துளசி), I'm currently a software developer in the advertising technology sector. I was born and raised in the Big Apple (NYC Baby!). I love all things tech and design. This is my blog, where I post my daily ramblings, reviews and commentary on trends about the tech and design world.
Site made with ♥ by Angie Makes