top of page

Python Strings: Humpty Dumpty Sat On A Wall

Python is the most popular programming language in fields like Artificial Intelligence, Machine Learning, and Space Exploration.


Observe:


Humpty Dumpty sat on a wall

The sun rises in the east.

My name is Pulkit.

Ram

Mohan

Alice

Bob_21


These are phrases, sentences, and words in English. In Python, we call them strings. Strings are sequences of characters and texts.


Quick Links


Strings
Strings

#1: Strings in Python


We use single ('), double ("), and triple (""" or ''') quotation marks to represent strings in Python. So, the above examples become:


'Humpty Dumpty sat on a wall'

"The sun rises in the east."

"""My name is Pulkit.

Ram

Mohan

Alice"""

"Bob_21"


Quotations Inside Strings

To include a quotation inside your string, use different quotation marks:

"My name is 'Pulkit'."

Here, we put 'Pulkit' inside single quotes and enclose the whole string in double quotes to distinguish them. Alternatively, you can use a backslash \ in this manner:

print("My name is \'Pulkit\'.")

Use \ before ' to include the single quote inside the string without ending it. The data type of strings is str. Let us see one more example:

book = "Harry Potter and the Philosopher's Stone"
author = "J. K. Rowling"
year = "1997"
plot = """
	A young boy, Harry Potter, discovers he is a wizard and       			attends Hogwarts, where he makes friends, learns magic, and 		uncovers the mystery of the Philosopher’s Stone. With the help of his friends, he stops the dark wizard Voldemort from returning to 		power.
"""

In the above example, we put the book name inside "" and assigned it to book variable. Similarly, we assigned the author name, publication year, and the plot to variables author, year, and plot respectively. Notice we enclosed the plot inside triple quotation marks as it is multiline. Let us print them in a good way.

print(f"My favourite book is {book}.")

Inside the () we are using a f-string (f) before " in order to let Python knows the book inside {} is a variable name having something in it. f is an f-string. {} is a placeholder and it can contain variables, functions, modifiers, operations, and more. This gives:

My favourite book is Harry Potter and the Philosopher's Stone.

Similarly, you can print:

print(f"The book author is {author}, and it was published in {year}")
print(f"The plot: {plot}")

#2: What More Can We Do With Python Strings?


We can capitalise, convert to lowercase, and perform other things on strings using methods. Methods are functions tied to objects, like strings, and are called using dot notation. Methods are called on objects, and they can be done in one line like this.


Syntax:

variable.method()

Use the variable name, followed by a dot (.), and then the method name with ().


Let us see some string methods.

name = " ramchandra mohan "

We put a string  ramchandra mohan inside a variable called name. Notice the intentional spaces before and after the string. We can remove them using the strip() method:

# Removes left and right whitespaces only 
name = name.strip() 

The strip() method is called using a dot notation (.) on the variable name. It strips (or removes) the leading (starting) and trailing (ending) whitespaces from the name. The output is assigned to the same variable name (we can do this in Python). Let us print it.

print(name)

# You'll get
ramchandra mohan

Capitalising the First Letter

The name is currently in lowercase. Let us capitalise only the first letter r. We have another method called capitalize(). We will use it in the similar way:

name.capitalize()

# You'll get
Ramchandra mohan

Only the first letter "r" is changed to "R", while the rest of the string remains unchanged.


Capitalising the First Letter of Each Word

Let us capitalise the first letter r and m.

# Capitalises the first letter of each word in the name
name = name.title()

# You'll get
Ramchandra Mohan

There are more string methods as well. 

Converts the string to all uppercase

name.upper()

Coverts the string to all lowercase

name.lower()

Breaks down a string into words or characters which are separated by a space.

name.split()

Removing the right whitespace

name.rstrip()

Removing the left whitespace

name.lstrip()

Removing a suffix

removesuffix()

Removing a prefix

removeprefix()

Let us count the number of repetitions of text in something. The syntax is:

.count(value, startposition, endposition) 

# counts number of text repetitions 

There are other commonly used string methods that we did not discuss above. You can be find here.


The methods we discussed above can be further modified by passing in different arguments, which we will see in the next post.


References


Sources


Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating

Most Read

AI Nine O Subscription

Subscribe to AI Nine O for 90-second learning.

Venus will reply here...

Your question...

VenusMoon Logo

Promoting equitable education for all through the fruitful use of AI.

© 2024-25 by VenusMoon Education | Udyam Registration Number: UDYAM-MP-10-0030480

bottom of page