Skip to main content

follow us

Python Strings

A string is simply a series of characters. Anything inside quotes is considered a string in Python, and you can use single or double quotes around your strings like this (MATTHES, 23).

"This is a string."
'This is also a string.'


The difference between double and single quotation marks is that using single quotation marks and an apostrophe in string will create an error as it thinks the string ends at the apostrophe. Double quotations marks prevent this.

>>>print ('we're going to the store')
SyntaxError: invalid syntax

>>>print ("we're going to the store")
we're going to the store


You can use a backward slash (Backslash: escape characters, it's going to escape any sort of functionality of that character and just leave it as text or data.) before the apostrophe to prevent the error.

>>>print (we\'re going to the store')
we're going to the store


Concatenation is a process of taking a string and appending it with other string on to it (put two string directly together). Python uses the plus symbol (+) to combine strings. You will see the result with no space. To avoid that, put space inside the word you want to concatenation.

>>> print ('Hi' + 'there')
Hithere

>>> print ('Hi' + ' there')
Hi there


The difference between using (+) plus sign and (,) comma sign
if you use a comma, it will automatically put space, but if you use the plus sign, it will combine those strings with no space

print("spam","eggs")
spam eggs

print("spam"+"eggs")
Then your output is: spameggs


Add number as string in concatenation? We can use a number as a string by using a quote between the number, for example; "23", "-5", so Python will see it as a string.

Strings can also be multiplied by integers. This produces a repeated version of the original string. The order of the string and the integer doesn't matter, but the string usually comes first. But, strings can't be multiplied by other strings, and strings also can't be multiplied by floats, even if the floats are whole numbers (sololearn).

>>> print ("spam" * 3)
spamspamspam

>>> 4 * '2'
'2222'

>>> '12' * '36' # error cause by multiplying a string with another string
TypeError: can't multiply sequence by non-int of type 'str'

>>> 'pythonisfun' * 6.5 # error cause multiply by float
TypeError: can't multiply sequence by non-int of type 'float'


You can perform multiple operations on strings simultaneously i.e. both concatenation and multiplication. Kind of like PEMDAS, but on strings,

>>> print(("No" + " ")*10)
No No No No No No No No No No


Summary:
str + int --> Error
str * str --> Error
str + str --> strstr
str * int --> strstrstr... int multiple


Changing Case in a String with Methods.
Many times you don’t want to trust the capitalization that your users provide, and you maybe want to convert strings to lowercase or uppercase before storing them. Changing the case of the words in a string is one of the simplest tasks you can do.

The method title() appears after the variable in the print() statement.

name = "maleo sanjaya"
print(name.title())


A method is an action that Python can perform on a piece of data. The dot (.) after name in name.title() tells Python to make the title() method act on the variable name. 

Every method is followed by a set of parentheses because methods often need additional information to do their work. That information is provided inside the parentheses. The title() function doesn’t need any additional information, so its parentheses are empty.

title() displays each word in title case, where each word begins with a capital letter. So, when you save this file and then run it. You should see this output:

Maleo Sanjaya


Using concatenation with .title() method - You can also use concatenation to compose complete messages using the information you’ve stored in a variable. 

Let’s look at an example:
first_name = "maleo"
last_name = "sanjaya"
full_name = first_name + " " + last_name
print("Hello, " + full_name.title() + "!")


Here, the full name is used in a sentence that greets the user, and the title() method is used to format the name appropriately. This code returns a simple but nicely formatted greeting:

Hello, Maleo Sanjaya!


You can also use concatenation to compose a message and then store the entire message in a variable:

first_name = "maleo"
last_name = "sanjaya"
full_name = first_name + " " + last_name
message = "Hello, " + full_name.title() + "!"
print(message)


This code displays the message “Hello, Maleo Sanjaya!” as well, but storing the message in a variable makes the final print statement much simpler.

upper() and lower() - The lower() and upper() method is particularly useful for storing data. You can also change a string to all uppercase or all lowercase letters like this:

name = "Maleo Sanjaya"
print(name.upper())
print(name.lower())


This will display the following:

MALEO SANJAYA
maleo sanjaya


References:
www.sololearn.com
MATTHES, ERIC. PYTHON CRASH COURSE (2ND EDITION). NO STARCH Press, 2018.

You Might Also Like:

Newest Post
Comment Policy: Be polite, and don't Spam, Thanks
open comment