Cenk Yildiran

Writing about lots of unrelated topics…


String Object Type in Python

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

Advertisements
One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

¤5.00
¤15.00
¤100.00
¤5.00
¤15.00
¤100.00
¤5.00
¤15.00
¤100.00

Or enter a custom amount

¤

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly


Leave a Reply

About Me

My name is Cenk, and I am an economist. I write on this internet site on economics, econometrics, finance, value-investing, programming, calculus, basketball, history, foods, books, self-improvement, well-being and productivity. This internet site is a personal blog, and the posts reflect my personal views and do not represent where I have been working.
For my academic works, please visit this site: https://cenkufukyildiran.academia.edu/
Posts related to financial markets, trading, investing and similar posts are not for financial advice purposes.

Newsletter

Discover more from Cenk Yildiran

Subscribe now to keep reading and get access to the full archive.

Continue reading