Python string

String inPythonIs a series of characters enclosed in quotation marks or double quotation marks:


You can assign string values to variables:

name = "Roger"

You can use to concatenate two strings+operator:

phrase = "Roger" + " is a good dog"

You can use the following to append to the string+=:

name = "Roger"
name += " is a good dog"

print(name) #Roger is a good dog

you can use itstrThe constructor of the class:

str(8) #"8"

This is essential for concatenating numbers to strings:

print("Roger is " + str(8) + " years old") #Roger is 8 years old

When using a special syntax definition, the string can be multi-line and enclosed in 3 quotation marks:

print("""Roger is


years old

#double quotes, or single quotes

print(’’’ Roger is 8 years old ‘’’)

Strings have a set of built-in methods, for example:

  • isalpha()Check if the string contains only characters and is not empty
  • isalnum()Check whether the string contains characters or numbers, and is not empty
  • isdecimal()Check if the string contains numbers and is not empty
  • lower()Get the lowercase version of the string
  • islower()Check if the string is lowercase
  • upper()Get the uppercase version of a string
  • isupper()Check if the string is uppercase
  • title()Get the uppercase version of the string
  • startsswith()Check if the string starts with a specific substring
  • endswith()Check if the string ends with a specific substring
  • replace()Replace part of the string
  • split()Split a string on a specific character delimiter
  • strip()Trim white spaces in strings
  • join()Append new letter to string
  • find()Find the position of the substring

There are a lot more.

None of these methods change the original string. They return a new, modified string. E.g:

name = "Roger"
print(name.lower()) #"roger"
print(name) #"Roger"

You can also use some global functions to process strings.

I especially thought oflen(), It gives you the length of the string:

name = "Roger"
print(len(name)) #5

ThisinOperators allow you to check whether a string contains substrings:

name = "Roger"
print("ger" in name) #True

Escaping is a method of adding special characters to a string.

For example, how to add double quotes to a string wrapped in double quotes?

name = "Roger"

"Ro"Ger"Won't work, because Python will think that the string starts with"Ro".

The feasible method is to use the string to escape the double quotes in the string.\Backslash character:

name = "Ro\"ger"

This also applies to single quotes\', And characters in special formats, such as\tFor labels,\nFor new line and\\Backslash.

Given a string, you can use square brackets to get its characters to give a specific item of its index starting from 0:

name = "Roger"
name[0] #'R'
name[1] #'o'
name[2] #'g'

Use negative numbers to count from the beginning:

name = "Roger"
name[-1] #"r"

You can also use ranges, the so-calledslice:

name = "Roger"
name[0:2] #"Ro"
name[:2] #"Ro"
name[2:] #"ger"

More python tutorials: