Python String
Strings in python are surrounded by either single quotation marks, or double quotation marks.
'guangshu' is the same as "guangshu".
You can display a string literal with the print() function
print('guangshu')
You can assign a multiline string to a variable by using three double quotes or three single quotes:
String as Array
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.
a = "Guangshu Coder"
print(a[1])
output: u
Looping Through a String
Since strings are arrays, we can loop through the characters in a string, with a for loop
Example:
for x in "guangshu":
print(x)
String Length
To get the length of a string, use the len() function.
The len() function returns the length of a string:
Example:
a = "Guangshu Coder"
print(len(a))
output: 14
#python #coding #programming
Slicing String
To get the length of a string, use the len() function.
The len() function returns the length of a string:
Example:
a = "Guangshu Coder"
print(len(a))
output: 14
Modify String
Python has a set of built-in methods that you can use on strings
upper() method returns the string in upper case:
b = "Guangshu Coder"
print(b.upper()) output: GUANGSHU CODER
lower() method returns the string in lower case:
print(b.lower()) output: guangshu coder
strip() method removes any whitespace from beginning or the end
print(b.strip()) output: Guangshu Coder
replace() method replaces a string with another string:
print(b.replace("G", "H")) output: Huangshu Coder
split() method splits string into substrings
if it finds instances of the separator.
print(split("s")) output: ['Guang', 'hu Coder']
Python Complete Course
Playlist: • Introduction to Python. Python code with e...
%%%%%%%%%%Code%%%%%%%%%%%%%%%%
python string
using single quotation marks
a = 'Guangshu Coder'
print(a)
using Double Quotation marks
a = "Guangshu Coder"
print(a)
Multiline String
using three double quotes
a = """
Guangshu Coder
Learn Code with me"""
print(a)
using three single quotes
a = '''
Guangshu Coder
Learn Code with me'''
print(a)
String Slicing
x = "Guangshu Coder"
print(x[2:5])
Slicing from start
print(x[:5])
slicing to end
print(x[2:])
negative indexing
print(x[-5:-2])
Modify String
a = "Guangshu Coder"
upper() method returns the string in upper case:
print(a.upper())
lower() method returns the string in lower case:
print(a.lower())
strip() method removes any whitespace from beginning or the end
print(a.strip())
replace() method replaces a string with another string:
print(a.replace("G", "H"))
split() method splits string into substrings if it finds instances of the separator.
print(a.split("s"))
Nesta página do site você pode assistir ao vídeo on-line Python String. String Functions Python code with example. Python Complete Course Video # 8 duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Guangshu Coder 30 Dezembro 2022, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 26 vezes e gostou like espectadores. Boa visualização!