Skip to main content

Command Palette

Search for a command to run...

Strings in Python

Published
•2 min read•View as Markdown
Strings in Python
B

I'm Proficient in a variety of Big Data technologies, including AWS, Azure, GCP, Hadoop, Databricks, Pyspark, Sql, Python, Docker, Jenkins, Git/GitHub, Kubernetes, Azure Data Engineering.

What are strings?

In Python, anything that you enclose between single or double quotation marks is considered a string. A string is essentially a sequence or array of textual data. Strings are used when working with Unicode characters.

Example

name = "abcd"

print("Hello, " + name)

Output

Hello, abcd

Note: It does not matter whether you enclose your strings in single or double quotes, the output remains the same.

Sometimes, the user might need to put quotation marks in between the strings. For example, consider the sentence: He said, “I want to eat an apple”.

How will you print this statement in Python?: He said, "I want to eat an apple". We will use single quotes for our convenience

print('He said, "I want to eat an apple".')

Multiline Strings

If our string has multiple lines, we can create them like this:

a = """Lorem ipsum dolor sit amet,

consectetur adipiscing elit,

sed do eiusmod tempor incididunt ut l

abore et dolore magna aliqua."""

print(a)

Accessing Characters of a String

In Python, the string is like an array of characters. We can access parts of a string by using its index which starts from 0.
Square brackets can be used to access elements of the string.

print(name[0])

print(name[1])

Looping through the string

We can loop through strings using a for loop like this:

for character in name:

print(character)

Above code prints all the characters in the string name one by one!

More from this blog

Untitled Publication

37 posts