ITVoyagers
25 subscribers
364 photos
6 videos
2 files
564 links
ITVoyagers is an educational blog for information technology and computer science program.
Website: itvoyagers.in
Download Telegram
String Datatype in PythonPython supports many simple and compound <a href="https://itvoyagers.in/python-program-comments-indentaton-expressions/">datatypes</a>.Few data types like list, dictionary are mutable and others like string datatype, tuple are immutable.Mutable datatypes means changes can be done and are reflected in same variable.Immutable datatypes means changes can be done but not in same variable.String is collection or sequence of characters.An empty string represents zero character or we can call it as null string.A string datatype can be defined with use of single or double quotes. Accessing a stringA string can be accessed using square brackets [].Example<pre>>>> itv="itvoyagers"
>>> itv[0]
'i'
>>> itv[6]
'g'
>>> itv[10]
Traceback (most recent call last):
File "<pyshell#3>", line 1, in
itv[10]
IndexError: string index out of range
>>> </pre> Above example consist of a string having 10 character and index position starting from 0 till 9. To access a string we need to pass index value inside square brackets to retrieve character located on that index. As last index number is 9 we get and IndexError if we pass an index greater than 9. So first letter ‘i’ is at position 0, second letter ‘t’ is at position 1 and so on..Example:<pre>>>> itv="itvoyagers"
>>> itv[2.5]
Traceback (most recent call last):
File "<pyshell#5>", line 1, in
itv[2.5]
TypeError: string indices must be integers
>>> </pre>From above example it is clear that we cannot use a float value ad index. An index should always be an integer and last value of index will always be n-1 if total length is n. len function or len()Python provides a built-in function called as len() to calculate number of characters in a string or simply we can say length of a string.We cannot calculate length of integers using len(). To calculate length of integers we must type cast it in string and then apply len() .Example:<pre>>>> itv="itvoyagers"
>>> len(itv)
10
>>> itv2="itvoyagers is an educational blog"
>>> len(itv2)
33
>>> itv3=""
>>> len(itv3)
0
>>> itv4=" "
>>> len(itv4)
1
>>> </pre>If we wish to get last letter of the string we might script it as follows:<pre>>>> itv="itvoyagers"
>>> h=len(itv)
>>> last=itv[h]
Traceback (most recent call last):
File "<pyshell#16>", line 1, in
last=itv[h]
IndexError: string index out of range
>>> </pre>This raises an IndexError. So to over come this we can modify code using following script:<pre>>>> itv="itvoyagers"
>>> h=len(itv)
>>> last=itv[h-1]
>>> last
's'
>>> </pre> Negative IndexingWe can use negative indexing for string datatype which will help us to count backward from the end of the string.If we use itv[-1] it will give output as ‘s’.<pre>>>> itv="itvoyagers"
>>> itv[-1]
's'
>>> itv[-2]
'r'
>>> itv[-9]
't'
>>> itv[-10]
'i'
>>> itv[-11]
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
itv[-11]
IndexError: string index out of range
>>> </pre>In negative indices, index begins with -1 and not 0 so last value of indexing will be -10 in our example and not -9. Traversing string using for loopTraversing is a process of moving in an iterable and locating values from index position.In this case our iterable is of type string and loop used is foe loop.<pre>>>> itv="itvoyagers"
>>> for i in itv:
print("Letter is",i)</pre>Output:<pre>Letter is i
Letter is t
Letter is v
Letter is o
Letter is y
Letter is a
Letter is g
Letter is e
Letter is r
Letter is s</pre> String slicingA string datatype can be sliced using [ : ] .Syntax:<pre>[start : end : step]</pre>This provide start and end value.Sometimes a step value is also mentioned after end value using colon separation.In below example we will see multiple outputs or multiple ways to slice a string datatype. Example:<pre>#string slicing
demo_string = "Itvoyagers is a blog"
print('demo_string[0:5]',demo_string[0:5])
print('demo_string[9:12]',demo_string[9:12])
print('demo_string[5:12]',demo_string[5:12])
print('demo_string[:3]',demo_string[:3])
print('demo_string[3:]'…
Hey guys check out
Dictionary methods in python 😎🧠
https://ift.tt/GQrajiz

Click on the link in bio

Do Visit our website
👉👉 itvoyagers.in 👈👈 to learn basic and advanced programming.

Do checkout our study materials for information technology and computer science program 📝📚🖥️

Comment you opinions and follow us on @itvoyagers_official

Thanks for all your support ❤️🤖

#itvoyagers #keepvoyaging #keepexploring #facts #python #educational #mumbai #itblog #explorer #studymaterial #programming #informationtechnology #computerscience #trending #quiztime #quiz #python #todolist #string #pythondictionary #developer #coder #development #coding #computer #c #java #website #learn #free