The string is a sequence of character that enclosed with single and double quotes . the python allows a string as a input then convert that into str id by using str id() method. the string is collection of text that is you to manipulate and work with the text in a python . Example in the below
text="Darshan"
print(id(text))
1647296430272
If the two string id as same then we denoted as a both variable assigned string is same . Example in the below
text1="darshan"
text2="darshan"
print(id(text1))
print(id(text2))
1879657338736
1879657338736
When we want to create a string we must be placed with in the double ,single and triple quotes . The python allows a you to create a text as string then perform that task you. and the python provides a three ways to create a string that is in the below
Single Quote ''
text='darshan'
print(text)
darshan
Double Quote ""
text="darshan"
print(text)
darshan
Triple Quote """ """ /'''
text="""darshan" ""
print(text)
darshan
The string is a collection of character that enclosed with double or single quotes . by using the index value then we access the individual character in the string . as below we created understand example in the below
The index value start form zero . ans in the string we can access the string character by using the index value at the first must character. the index value placed with in the [] brackets
Positive index Start from 0 at the first must string character
Negative index stat from -1 at the last of the string character
The both Example in the below
Positive Example
Text="welcome to python code
print(text[5:12])
me to p
Negative Example
Text="welcome to python code
print(text[-12:-5])
python
The python provides the str() to convert the any data type to string format. the str() is a built in function is used to convert the object in to string format. str() method provide to convert the effective data< types to a string format
text=str(234)#converting in to string
text2=str(235)#converting in to string
print(text1+text2) # concatenating the two strings
234235
As we know the string is a sequence of character that is enclosed with the double quote
The Python provides the some operations . That is in the below
The concatenating in a python adding the two string by using a "+" operator we we want to add some string into single line we must be using + operator and it is also called joining strings
There are two ways to adding the string in a python
str1="Hello,"
str2=" Coders how are You"
str3=str1+str2
print(str3)
>Hello, Coders how are you
str1="I,"
str2=" Love"
str3=" Coding"
print(str1 +str2 +str3)
I, Love Coding
The join() function is also used to add the two strings and the join method is help full for sequence types manipulation . the join() function is iterable it it repeating as long as string are sequence elements get ends
tr1="I,"
str2=" Love"
str3=" Coding"
join="->"
print(str1 +str2 +str3.join(join))
I, Love- Coding>
The python allow a us to repeat the string as we require by using string name and followed by * (asterisk) . understand Example
text="Darshan"*4
Darshan Darshan Darshan Darshan
membership in The as we saw in previous operator chapter . the membership operator is when the two variable storing the same string the it result will be true otherwise the result will be false . the membership operator compare the two variable the first variable is assigned some value then the second variable assigned value as same then the result will be true . the result is boolean value either true nor false. Example in the below.
` Text1="Darshan"
Text2="Darshan"
print(Text in Text2)
True
text="Hello World code "
print("Hello" in text )
True
Text1="Darshan"
Text2="Darshan"
print(Text not in Text2)
False
text="Hello World code "
print("Hello" not in text )
False
we can compare string by using the comparison operators =,!=,<,>,>= and other the result will be boolean form either True or False. the Example in the below
text1="Darshan coder"
text2="Darshan"
print(text1<=text2)
print(text1>text2)
print(text1==text2)
print(text1!=text2)
False
True
False
True