A string is a series of characters and is used to store and represent texts in Python.
You can download the code below:
>>> T = 'Text'
>>> print(T)
Text
>>> t = "text"
>>> print(t)
text
>>> print(T)
Text
>>> print(len(T))
4
>>> print(T[0])
T
>>> print(T[-1])
t
>>> print(T[-2])
x
>>> print(len(T))
4
>>> print(len(T) - 1)
3
>>> print(T[len(T)-1])
t
>>> print(T)
Text
>>> print(T[1:3])
ex
>>> print(T[1:])
ext
>>> print(T[0:3])
Tex
>>> print(T[ : 3])
Tex
>>> print(T[ : ])
Text
>>> print(T + ' to read!')
Text to read!
>>> print(T * 3)
TextTextText
>>> T[0] = 'm'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> print(T)
Text
>>> T = 'm' + T[1:]
>>> print(T)
mext
>>> L = list(T)
>>> print(L)
['m', 'e', 'x', 't']
>>> L[2] = 'z'
>>> print(L)
['m', 'e', 'z', 't']
>>> print(''.join(L))
mezt
>>> L2 = ''.join(L)
>>> print(L2)
mezt
>>> siblings = 3
>>> print(siblings)
3
>>> info = "You have " + siblings + " !"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> info_2 = "You have " + str(siblings) + " !"
>>> print(info_2)
You have 3 !
Below are a few book suggestions:
Eric Matthes, Python Crash Course, A Hands-On, Project-Based Introduction to Programming: https://amzn.to/41Mjn2T
Mark Lutz, Learning Python: https://amzn.to/3V1UPAW
Luciano Ramalho, Fluent Python: https://amzn.to/41RCCIy
Alex Martelli, Python in a Nutshell: https://amzn.to/41SFAfR
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearly
Leave a Reply