Python Strings


Strings

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:


Example
print("Hello")
print('Hello')


Assign String to a Variable

Giving a unit of flexibility is done with a variable name followed by an equal symbol and a unit of characters:


Example
a = "Hello"
print(a)

Multiline Strings

You can assign a multi-line character unit to variables by using three quotes:


Example

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:


Example
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.



Strings are Arrays

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


Example

Get the character at position 1 (remember that the first character has the position 0):

a = "Hello, World!"
print(a[1])


Looping through a String

Since the strings are frames, we can enter between the strings in the strings unit, with a for loop.


Example

Loop through the letters in the word "banana":

for x in "banana":
  print(x)


String Length

To find the length of a character unit, use the len() function


Example

The len() function returns the length of a string:

a = "Hello, World!"
print(len(a))


Check String

To check if a particular phrase or character exists in a character unit, we can use the keyword in.


Example

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:


Example

Print only if "free" is present:

txt = "The best things in life are free!"
if "free" in txt:
  print("Yes, 'free' is present.")


Check if NOT

To check whether a particular phrase or letter is NOT a character unit, we can use the not in keyword.


Example

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:


Example

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.")



Python - Slicing Strings



Slicing

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.


Example

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.



Slice from the Start

With the exception of the first index, the range will start with the first letter:


Example

Get the characters from the start to position 5 (not included):

b = "Hello, World!"
print(b[:5])


Slice To the End

By excluding the last index, the scope will come to an end:


Example

Get the characters from position 2, and all the way to the end:

b = "Hello, World!"
print(b[2:])


Negative Indexing

Use negative clues to start the piece from the end of the series:


Example

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 - Modify Strings



Python has a set of built-in methods that you can use on strings


Upper Case


Example

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

a = "Hello, World!"
print(a.upper())


Lower Case


Example

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

a = "Hello, World!"
print(a.lower())


Remove Whitespace

Whitespace space before and / or after the original text, and you usually want to clear this space.


Example

The strip() method removes any whitespace from the beginning or the end:

a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"


Replace String


Example

The replace() method replaces a string with another string:

a = "Hello, World!"
print(a.replace("H", "J"))


Split String

The split() method returns a list where the text between the specified separator becomes a list item.


Example

The split() method splits the string into substrings if it finds instances of the separator:

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