Skip to content
  • About
  • Courses
  • ResearchExpand
    • Research Publications
    • Books
    • Patents
    • Ph.D. Supervised
  • Workshop/Conferences
  • ToolsExpand
    • Creative Image Converter
    • Creative QRCode Generator
    • Creative QR Code Generator Tool
    • EMI Calculator
    • SIP Calculator
  • Blog
  • Resume
One Page CV
Python

String Data Type

A string is a sequence of characters.

String Data Type

str1="hello"
print(type(str1))
Ans: <class ‘str’>

str2='123'
print(type(str2))
Ans: <class ‘str’>
str2='123'
ch=int(str2)
print(type(ch))
Ans: <class ‘int’>

Adding and Concatenation using ‘+’

Adding to the two string using the ‘+’

str1="hello"
str2='paris'
add1=str1+str2
print(add1)
Ans: Hello Paris

Adding to the two string using the ‘+’ but even the numeric values here is also a string adding any numeric value to it will cause error.

str2='123'
ch=str2+1
print(ch)
Ans: Error String type cannot add any numeric 1.

Before adding any value first it needs to change its type ‘int’, then any addition can take place.

str2='123'
ch=int(str2)+1
print(ch)
Ans: 124
s=’1’+’1′
print(s)
Ans: ’11’
 
s=’1’+’1′
print(int(s)+1)
Ans: True

Comparison

'Hello'=='Hello'
Ans: True
 
'Hello'=='hello'
Ans: False
 
'1'==1
Ans: False
 
'1'=='1'
Ans: True

Array

fruit='apple'
print(fruit[1])
Ans: ‘p’

fruit='banana'
x=2
print(fruit[x+2])
Ans: ‘n’

For Loop

fruit='apple'
for i in range(0,5):
    print(fruit[i])
Ans:
a
p
p
l
e
fruit='apple'
for i in range(0,6):
    print(fruit[i])
Ans:
a
p
p
l
e
Error: No index value
for i in range(0,len(fruit)):
    print(fruit[i])
Ans:
a
p
p
l
e 
fruit='apple'
for letter in fruit:
    print(letter)
Ans:
a
p
p
l
e 

To find the response of array in the one line then ‘sys’ library can be used

import sys
for i in range(0,len(fruit)):
    sys.stdout.write(fruit[i])
Ans: apple

While Loop with the len function

fruit='apple'
i=0
while i<len(fruit):
    ch=fruit[i]
    print(i,ch)
    i=i+1
Ans:
0 a
1 p
2 p
3 l
4 e
word='banana'
count=0
for letter in word:
    if letter=='a':
    count=count+1
    print(count)
Ans:
1
2
3

String Functions
Length of the string

fruit='apple'
len(fruit)
Ans: 5

Creating Arrays from the line

str1 = "I love my country"
str1[0:]
Ans: 'I love my country'
 
str1[0:10]
Ans: 'I love my'
 
str1[3:4]
Ans: 'o'

Post navigation

Previous Previous
String Library
NextContinue
Compute the Compound Interest.py
  • Latest

    [professional_resume]

    Read More Continue

  • Latest

    Explore Generative AI with the Gemini API in Vertex AI

    Read More Explore Generative AI with the Gemini API in Vertex AIContinue

  • Latest

    Inspect Rich Documents with Gemini Multimodality and Multimodal RAG

    Read More Inspect Rich Documents with Gemini Multimodality and Multimodal RAGContinue

  • Latest

    🎓 Why Original Work Matters in Your Final Year Project (And How It Can Shape Your Career)

    In engineering colleges across the country, final year projects are often treated as just another academic task. But what many students fail to realize is…

    Read More 🎓 Why Original Work Matters in Your Final Year Project (And How It Can Shape Your Career)Continue

  • Latest

    🎓 How to Choose Your Final Year Project: A Practical Guide for BTech Students

    Choosing the right final year project is one of the most important decisions of your engineering journey. It’s more than just a submission — it’s…

    Read More 🎓 How to Choose Your Final Year Project: A Practical Guide for BTech StudentsContinue

Nishant Munjal

Coding Humanity’s Future </>


Facebook Twitter Linkedin YouTube Github Email

Tools

  • SIP Calculator
  • EMI Calculator
  • Creative QR Code
  • Image Converter

Resources

  • Blog
  • Contact
  • Refund and Returns

Legal

  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

© 2025 - All Rights Reserved

  • About
  • Courses
  • Research
    • Research Publications
    • Books
    • Patents
    • Ph.D. Supervised
  • Workshop/Conferences
  • Tools
    • Creative Image Converter
    • Creative QRCode Generator
    • Creative QR Code Generator Tool
    • EMI Calculator
    • SIP Calculator
  • Blog
  • Resume
Download CV
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.