Python Strings, Slicing and other Function Tutorial # 1 How to use string in python

Published: 16 June 2023
on channel: technical learning
2
0

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.


On this page of the site you can watch the video online Python Strings, Slicing and other Function Tutorial # 1 How to use string in python with a duration of hours minute second in good quality, which was uploaded by the user technical learning 16 June 2023, share the link with friends and acquaintances, this video has already been watched 2 times on youtube and it was liked by 0 viewers. Enjoy your viewing!