Python str (String) Data Type

Python str (string) is a sequence of characters.

>>> symbol = 'AAPL'
>>> type(symbol)
<class 'str'>

You can find a character within a string by indexing. Position starts with zero. Negative numbers start from the end of string.

>>> symbol = 'MSFT'
>>> symbol[0]
>>> symbol[1]
>>> symbol[-1]

You can find string length (number of characters) using the len() function.

>>> len('MSFT')
>>> len('PyTut')

Strings can be typed with single or double quotes. They are equivalent.

>>> single = 'AAPL'
>>> double = "AAPL"
>>> single == double

A string can be empty and still a string.

>>> empty = ''
>>> type(empty)
<class 'str'>

By remaining on this website or using its content, you confirm that you have read and agree with the Terms of Use Agreement.

We are not liable for any damages resulting from using this website. Any information may be inaccurate or incomplete. See full Limitation of Liability.

Content may include affiliate links, which means we may earn commission if you buy on the linked website. See full Affiliate and Referral Disclosure.

We use cookies and similar technology to improve user experience and analyze traffic. See full Cookie Policy.

See also Privacy Policy on how we collect and handle user data.

© 2024 PyTut