Skip to content

Latest commit

 

History

History
174 lines (149 loc) · 8.42 KB

File metadata and controls

174 lines (149 loc) · 8.42 KB

#programming #programming/python #semester-1

Strings

Strings in python are surrounded by either single or double quotes:

x = "hi"
y = 'hello'

Strings are arrays, therefore you can access the characters in a string using:

string = "a string"
x = string[0] # "a"
y = string[1] # " "
z = string[2] # "s"

You can use the len() function to get the length of a string:

x = "string"
print(len(string)) # 6

Multiline Strings

You can write a multiline string with 3 sets of quotes:

print(""" hi
this is a multiline
string
bye """)

String Slicing

We can return a range of characters from a string:

string = "Hello World!"
print(x[2:5]) # Characters from the position to position 5 -> "llo"
print(x[:5]) # Characters from the start to position 5 -> "Hello"
print(x[2:]) # Characters from position 2 to the end -> "llo World!"
print(x[-5:-2]) # Characters from the end -5 to the end -2 -> "orl"

String Methods

The upper() method returns the string in upper case:

string = "Hello World!"
print(string.upper()) # "HELLO WORLD!"

The lower() method returns the string in lower case:

string = "Hello World!"
print(string.lower()) # "hello world!"

The strip() method removes the whitespace around the string:

string = " Hello World! "
print(string.strip()) # "Hello World!"

The replace() method replaces the characters in a string:

string = "Hello World!"
print(string.replace("H", "J")) # "Jello World!"

The split() method splits the string if it finds the seperator:

string = "Hello, World!"
print(string.split(",")) # ['Hello', ' World']

String Concatenation

To concatenate (combine) 2 strings you can add them together:

a = "Hello"
b = "World"
c = a + b # HelloWorld
d = a + ' ' + b # Hello World

F Strings

Formatting strings allow you to add curly brackets as a placeholder for variables and other operations:

name = "Jim"
x = f"My name is {Jim}"

A placeholder can contain variables, operations and functions.

A placeholder can have a modifier to format the value:

price = 59
txt = f"The price is {price:2f} euro"
print(txt) # "The price is 59.00 euro"

Escape Characters

To insert illegal characters in a string, we use the escape character: \

string = "This is a \"quote\" in a string"

Here is a list of the escape characters:

Code Result
' Single Quote
\ Backslash
\n New Line
\r Carriage Return
\t Tab
\b Backspace
\f Form Feed
\ooo Octal value
\xhh Hex value

List of All String Methods

Here is a list of all string methods:

Method Description
capitalize() Converts the first character to upper case
casefold() Converts string into lower case
center() Returns a centered string
count() Returns the number of times a specified value occurs in a string
encode() Returns an encoded version of the string
endswith() Returns true if the string ends with the specified value
expandtabs() Sets the tab size of the string
find() Searches the string for a specified value and returns the position of where it was found
format() Formats specified values in a string
format_map() Formats specified values in a string
index() Searches the string for a specified value and returns the position of where it was found
isalnum() Returns True if all characters in the string are alphanumeric
isalpha() Returns True if all characters in the string are in the alphabet
isascii() Returns True if all characters in the string are ascii characters
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
isidentifier() Returns True if the string is an identifier
islower() Returns True if all characters in the string are lower case
isnumeric() Returns True if all characters in the string are numeric
isprintable() Returns True if all characters in the string are printable
isspace() Returns True if all characters in the string are whitespaces
istitle() Returns True if the string follows the rules of a title
isupper() Returns True if all characters in the string are upper case
join() Joins the elements of an iterable to the end of the string
ljust() Returns a left justified version of the string
lower() Converts a string into lower case
lstrip() Returns a left trim version of the string
maketrans() Returns a translation table to be used in translations
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a specified value
rfind() Searches the string for a specified value and returns the last position of where it was found
rindex() Searches the string for a specified value and returns the last position of where it was found
rjust() Returns a right justified version of the string
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
rstrip() Returns a right trim version of the string
split() Splits the string at the specified separator, and returns a list
splitlines() Splits the string at line breaks and returns a list
startswith() Returns true if the string starts with the specified value
strip() Returns a trimmed version of the string
swapcase() Swaps cases, lower case becomes upper case and vice versa
title() Converts the first character of each word to upper case
translate() Returns a translated string
upper() Converts a string into upper case
zfill() Fills the string with a specified number of 0 values at the beginning