Welcome To
Strings


String in Python

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

Example

text="Darshan"
print(id(text))

Output

1647296430272

If the two string id as same then we denoted as a both variable assigned string is same . Example in the below

Example

text1="darshan"
text2="darshan"
print(id(text1))
print(id(text2))

Output

1879657338736
1879657338736

Creating string

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

Example

Single Quote ''

text='darshan'
print(text)

Output

darshan

Double Quote ""

text="darshan"
print(text)

Output

darshan

Triple Quote """ """ /'''

text="""darshan" ""
print(text)

Output

darshan

Accessing part of String value By Using Index

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

Index

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])

Output

me to p

Negative Example

Text="welcome to python code
print(text[-12:-5])

Output

python

str() Function

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

Example

text=str(234)#converting in to string
text2=str(235)#converting in to string
print(text1+text2) # concatenating the two strings

Output

234235

String Operations

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

  1. concatenating
  2. Repitition
  3. Membership
  4. Comparing
  5. Slicing

Concatenating

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

  1. Add two or more strings and assign its value to a another string
  2. Concatenate two or more string while printing the output

Example1

str1="Hello,"
str2=" Coders how are You"
str3=str1+str2
print(str3)

OUtput

>Hello, Coders how are you

Example2

str1="I,"
str2=" Love"
str3=" Coding"
print(str1 +str2 +str3)

Output

I, Love Coding

Join() Function

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

Example

tr1="I,"
str2=" Love"
str3=" Coding"
join="->"
print(str1 +str2 +str3.join(join))

Output

I, Love- Coding>

Repeating Of a string

The python allow a us to repeat the string as we require by using string name and followed by * (asterisk) . understand Example

Example

text="Darshan"*4

Output

Darshan Darshan Darshan Darshan

Membership Operator (in and not in )

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.

`

Examples

Text1="Darshan"
Text2="Darshan"
print(Text in Text2)

Output

True

Example2

text="Hello World code "
print("Hello" in text )

Output

True

Not in membership

Examples

Text1="Darshan"
Text2="Darshan"
print(Text not in Text2)

Output

False

Example2

text="Hello World code "
print("Hello" not in text )

Output

False

Comparing Strings

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

Example

text1="Darshan coder"
text2="Darshan"
print(text1<=text2)
print(text1>text2)
print(text1==text2)
print(text1!=text2)

Output

False
True
False
True