Python Strings - String Type Tutorial with Examples - APPFICIAL

Publicado em: 08 Setembro 2021
no canal de: Appficial
949
7

A string is a data type, and string variables can hold sequences of text. A string literal is the actual string value used inside your python program. A programmer creates a string literal by surrounding text with single or double quotes. Ex: name = ‘Bob’, or city = other_city, where other_city is a string variable.

The string data type is also called a sequence type because it holds characters in a specific order. For example, the word ‘Hello’ is a string, where the H is at index position 0, the e is at index 1, the l’s are at 2 and 3, and the o is a index 4. You can access any of these characters by placing the index in brackets. Ex:

word = ‘Hello’
print(word[0], word[4])

You can use the input() function to get strings from user input, while your program is running. Ex:

print(‘What is your name?’)
name = input()
print(‘Hello’, name)

The length of a string refers to the number of characters. For example, ‘Bob’ has a length of 3. To get the length of a string in your program, use the len() function.

Strings are immutable, which means that you cannot change the characters of a string’s value. Functions that appear to alter a string, actually just returns back an all new string, since they cannot change the original string. You can assign a string variable to a new string or to a function call that returns a new string.

name = ‘James’
print(name.upper())
print(name) # not uppercase this time
name = name.upper()
print(name) # is uppercase this time

You can glue strings together using the + operator. This is called string concatenation. Ex: name = ‘Tony’ + ‘ Montana’.

Note that the result of string concatenation is a new string, so the original string is not changed.

Subscribe to Appficial for more programming videos coming soon. Also, don't forget to click LIKE and comment on the video if it helped you out!


Nesta página do site você pode assistir ao vídeo on-line Python Strings - String Type Tutorial with Examples - APPFICIAL duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Appficial 08 Setembro 2021, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 949 vezes e gostou 7 espectadores. Boa visualização!