Social Media

1/02/2019

Introduction To String

String is a sequential data structure.
Indexing and slicing is supported by String.
It is immutable.
String also support negative indexing. Negative indexing start from -1 in the last.
example:
s="Mayank"
print(s,type(s)) # Mayank <class 'str'>
print(s[-2]) # n

How to take string from user??
String can simply be taken from user using input() function.
s=input()
print(s)
String also support concatenation. Means we can concatenate a string to another string also.
s="Mayank"
print(id(s)) 
s+="Prajapati"
print(id(s))
Both will print different id which shows that string is immutable data structure.

Conversion of list into string:
We can convert a whole list into a string. This can be done simply using the join method. A simple script which will illustrate this concept is given below:
l=["mayank", "kumar", "prajapati"]
s=" ".join(l)
print(s) # mayank kumar prajapati

Be First to Post Comment !
Post a Comment