Strings
Strings in python are surrounded by either single quotation marks, or double quotation marks.
Assigning a string to a variable is done with the variable name followed by an equal sign and the string:
Multiline Strings
You can assign a multiline string to a variable by using three quotes.
Note: in the result, the line breaks are inserted at the same position as in the code.
Strings are Arrays
Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters.
However, Python does not have a character data type, a single character is simply a string with a length of 1.
Square brackets can be used to access elements of the string.
Looping Through a String
Since strings are arrays, we can loop through the characters in a string, with a for loop.
Check String
To check if a certain phrase or character is present in a string, we can use the keyword in.
Slicing
You can return a range of characters by using the slice syntax.
Specify the start index and the end index, separated by a colon, to return a part of the string.
Slice From the Start
By leaving out the start index, the range will start at the first character:
Slice To the End
By leaving out the end index, the range will go to the end:
Negative Indexing
Use negative indexes to start the slice from the end of the string:
Python - Modify Strings
Python has a set of built-in methods that you can use on strings.
Upper Case
The upper() method returns the string in upper case.
Lower Case
The lower() method returns the string in lower case.
Remove Whitespace
Whitespace is the space before and/or after the actual text, and very often you want to remove this space.
The strip() method removes any whitespace from the beginning or the end.
Replace String
The replace() method replaces a string with another string.
The split() method returns a list where the text between the specified separator becomes the list items.
The split() method splits the string into substrings if it finds instances of the separator.
Auf dieser Seite können Sie das Online-Video Python Strings, Slicing and other Function Tutorial # 1 How to use string in python mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer technical learning 16 Juni 2023 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 2 Mal angesehen und es wurde von 0 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!