Python Tutorials
Python File Handling
Python Modules
Strings in the python are surrounded by either single quotation marks, or double quotation marks.
'hello' is the same as "hello".
You can display the actual character unit by print() function:
print("Hello")
print('Hello')
Giving a unit of flexibility is done with a variable name followed by an equal symbol and a unit of characters:
a = "Hello"
print(a)
You can assign a multi-line character unit to variables by using three quotes:
You can use three double quotes:
a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do
eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)
Or three quotes one:
a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do
eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
print(a)
Note: as a result, the line break is applied to the same location as the code.
Like many other popular programming languages, the cables in Python are a list of bytes representing unicode characters.
However, Python does not have a character type data, one character is simply a series of 1 length.
Square brackets can be used to access character unit features
Get the character at position 1 (remember that the first character has the position 0):
a = "Hello, World!"
print(a[1])
Since the strings are frames, we can enter between the strings in the strings unit, with a for loop.
Loop through the letters in the word "banana":
for x in "banana":
print(x)
To find the length of a character unit, use the len() function
The len() function returns the length of a string:
a = "Hello, World!"
print(len(a))
To check if a particular phrase or character exists in a character unit, we can use the keyword in.
Check if "free" is present in the following text:
txt = "The best things in life are free!"
print("free" in txt)
Use it in a statement if:
Print only if "free" is present:
txt = "The best things in life are free!"
if "free" in txt:
print("Yes, 'free' is present.")
To check whether a particular phrase or letter is NOT a character unit, we can use the not in keyword.
Check if "expensive" is NOT present in the following text:
txt = "The best things in life are free!"
print("expensive" not in txt)
Use it in a statement if:
print only if "expensive" is NOT present:
txt = "The best things in life are free!"
if "expensive" not in txt:
print("No, 'expensive' is NOT present.")
You can restore the width of the characters by using the syntax of the piece.
Specify the starting point and end point, separated by a colon, to replace part of the character unit.
Get the characters from position 2 to position 5 (not included):
b = "Hello, World!"
print(b[2:5])
Note: The first letter has 0 index.
With the exception of the first index, the range will start with the first letter:
Get the characters from the start to position 5 (not included):
b = "Hello, World!"
print(b[:5])
By excluding the last index, the scope will come to an end:
Get the characters from position 2, and all the way to the end:
b = "Hello, World!"
print(b[2:])
Use negative clues to start the piece from the end of the series:
Get the characters:
From: "o" in "World!" (position -5)
To, but not included: "d" in "World!" (position -2):
b = "Hello, World!"
print(b[-5:-2])
Python has a set of built-in methods that you can use on strings
The upper() method returns the string in upper case:
a = "Hello, World!"
print(a.upper())
The lower() method returns the string in lower case:
a = "Hello, World!"
print(a.lower())
Whitespace space before and / or after the original text, and you usually want to clear this space.
The strip() method removes any whitespace from the beginning or the end:
a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"
The replace() method replaces a string with another string:
a = "Hello, World!"
print(a.replace("H", "J"))
The split() method returns a list where the text between the specified separator becomes a list item.
The split() method splits the string into substrings if it finds instances of the separator:
a = "Hello, World!"
print(a.split(",")) #
returns ['Hello', ' World!']