Top Posts
Intranet Application Case Studies
Electronic E-commerce and the Trade Cycle
Types of JDBC drivers
C++ Program to implements Constructor Overloading
C++ Program to implements Constructor
C++ Program to calculate the area using classes
C++ Class Program to Store and Display Employee...
Operator Overloading of Decrement — Operator
Postfix Increment ++ Operator Overloading
Top 8 Programming Languages That Will Rule in...
TECHARGE
  • HOME
  • BLOGS
  • TUTORIALS
    • ALL TUTORIALS
    • PROGRAMMING TUTORIALS
      • JAVA TUTORIALS
      • C++ TUTORIAL
      • C PROGRAMMING TUTORIALS
      • PYTHON TUTORIAL
      • KNOWLEDGE MANAGEMENT TUTORIALS
      • DATA STRUCTURE AND ALGORITHM TUTORIALS
      • PROGRAMMING EXAMPLES
        • CPP EXAMPLES
        • JAVA EXAMPLES
        • C++ GRAPHICS PROGRAM
    • PROJECTS
      • PYTHON PROJECTS
      • SWIFT PROJECT
    • PPROGRAMMING QUIZ
    • DBMS TUTORIALS
    • COMPUTER NETWORK TUTORIALS
    • COMPUTER NETWORK SECURITY TUTORIALS
    • E COMMERCE TUTORIALS 
    • AWS TUTORIAL
    • INTERNET OF THINGS
    • CHEATSHEET
  • MORE
    • JOBS AND INTERNSHIPS
    • INTERVIEW PREPARATION
    • TECH BOOK
    • TECH NEWS
    • INSTAGRAM GALLERY
    • UNIVERSITY PAPERS
    • MNC TWEETS
    • THINKECO INITIATIVES
    • WEB STORIES
    • CONTACT US
  • WRITE +
  • ABOUT US
Python Tutorial

String in Python

by anupmaurya May 14, 2022
written by anupmaurya 0 comment
1.3K

Table of Contents

    • Basic Python String
  • String Concatenation
  • String Replication
    • Math Operators

As string is an datatype .Here you come to know about how to create a string and different operation on String.

How to Create a String in Python

You can create a string in three ways using single, double or triple quotes. Here’s an example of every option:

Basic Python String

my_string = “Let’s Learn Python!”
another_string = ‘It may seem difficult first, but you can do it!’
a_long_string = ‘’’Yes, you can even master multi-line strings
that cover more than one line
with some practice’’’

IMP! Whichever option you choose, you should stick to it and use it consistently within your program.

As the next step, you can use the print() function to output your string in the console window.

This lets you review your code and ensure that all functions well. Here’s a snippet for that:

print(“Let’s print out a string!”)

String Concatenation

Concatenation — a way to add two strings together using the “+” operator. Here’s how it’s done:

string_one = “I’m reading “
string_two = “a new great book!”
string_three = string_one + string_two

Note: You can’t apply + operator to two different data types e.g. string + integer. If you try to do that, you’ll get the following Python error:

TypeError: Can’t convert ‘int’ object to str implicitly

String Replication

As the name implies, this command lets you repeat the same string several times. This is done using * operator. Mind that this operator acts as a replicator only with string data types. When applied to numbers, it acts as a multiplier.


String replication example:

‘Alice’ * 5 ‘AliceAliceAliceAliceAlice’

And with print ()

And your output will be Alice written five times in a row.

Math Operators

For reference, here’s a list of other math operations you can apply towards numbers:

OperatorsOperationExample
**Exponent2**3=8
%Modulus/Remainder6%2=0
//Integer division8//2=4
/Division6/3=2
*Multiplication6*3=18
+Addition5+4=9
–Subtraction5-2=3
Math Operators

How to Store Strings in Variables

Variables in Python 3 are special symbols that assign a specific storage location to a value that’s tied to it. In essence, variables are like special labels that you place on some value to know where it’s stored.
Strings incorporate data.

So you can “pack” them inside a variable. Doing so makes it easier to work with complex Python programs.
Here’s how you can store a string inside a variable

my_str = “Hello World”

Let’s break it down a bit further:

  • my_str is the variable name.
  • = is the assignment operator.
  • “Just a random string” is a value you tie to the variable name.

Now when you print this out, you receive the string output.

print(my_str)
= Hello World

See? By using variables, you save yourself heaps of effort as you don’t need to retype the complete string every time you want to use it.

string in python
Share 2 FacebookTwitterLinkedinRedditWhatsappTelegramEmail
anupmaurya

Hey there, My name is Anup Maurya. I was born with love with programming and worked with TCS. One of best global (IT) services and consulting company as System Administrator . I also love graphics designing. It's my pleasure to have you here.

previous post
Python Data Types
next post
Threads in Java

You may also like

Getting Started with Python

Python List Comprehension

Python Function

Python if else

Python Libraries

Python Dictionary

Python Input, Output, and Import

Top 10 Popular Python Interview Questions for freshers

Python List

Python Operators

PYTHON Tutorial

  • Online Python Compiler
  • Getting Started with Python
  • String in Python
  • Python Data Types
  • Python Operators
  • Python Keywords and Identifiers
  • Python Input, Output, and Import
  • Python if else
  • Python List
  • Python Dictionary
  • Python Libraries
  • Matplotib Tutorial
    • Python | Introduction to Matplotlib Library Tutorial
    • Python | Pyplot in Matplotlib Tutorial
    • Python | Ellipse, Pie Charts, Tables and Scatter Plot in Matplotlib
    • Python : PathPatch ,3D plotting & StreamPlot in Mathplotlib
    • Python: Plots, Images, Contour and Pseudocolor in Matplotlib

Keep in touch

Facebook Twitter Instagram Pinterest

Recent Posts

  • Intranet Application Case Studies

    March 21, 2023
  • Electronic E-commerce and the Trade Cycle

    March 21, 2023
  • Types of JDBC drivers

    December 28, 2022
  • C++ Program to implements Constructor Overloading

    December 27, 2022

EDUCATIONAL

  • Top 8 Programming Languages That Will Rule in 2023

  • Difference between Google Cloud Platform, AWS and Azure

  • Google Apps You Should Be Using in 2022

  • Top Sites From Where You Can Learn

  • PyScript: Python in the Browser

  • Best Fake Email Generators (Free Temporary Email Address)

  • How to Find Out Who Owns a Domain Name

  • Mobile phone brands by country of origin

  • How to start a new YouTube Channel in 2022

  • Best way to use google search you won’t believe exist

CHEATSHEET

  • Git and Github 2022 Cheat Sheet

  • ReactJs Cheatsheet

  • Linux Commands Cheat Sheet

  • C Programming language Cheatsheet

  • Scala Cheatsheet

  • MySQL Cheatsheet

  • Javascript Cheatsheet

PROJECTS

  • Print emojis using python without any module

  • Country Date and Time using Python

  • Covid-19 Tracker Application Using Python

  • Python | GUI Calendar using Tkinter

  • Shutdown Computer with Voice Using Python

  • Python GUI Calculator using Tkinter

  • Convert an Image to ASCII art using Python

  • Python YouTube Downloader with Pytube

  • Tic-Tac-Toe using Python

  • Draw Indian Flag using Python

  • Drawing Pikachu with the Python turtle library

  • Word Dictionary using Tkinter

TECH NEWS

  • 5+ Best Humanoid Robots In The World

  • Reliance Jio launches streaming platform JioGamesWatch

  • Microsoft Teams down for thousands of users

  • Carbon: Google programming language as a C++ successor

JOBS AND INTERNSHIPS

  • Accenture Off Campus Hiring Drive | Associate Job | Program Project Management | 2019-2022 Batch| Apply Now

    September 1, 2022

@2019-21 - All Right Reserved. Designed and Developed by Techarge

TECHARGE
  • HOME
  • BLOGS
  • TUTORIALS
    • ALL TUTORIALS
    • PROGRAMMING TUTORIALS
      • JAVA TUTORIALS
      • C++ TUTORIAL
      • C PROGRAMMING TUTORIALS
      • PYTHON TUTORIAL
      • KNOWLEDGE MANAGEMENT TUTORIALS
      • DATA STRUCTURE AND ALGORITHM TUTORIALS
      • PROGRAMMING EXAMPLES
        • CPP EXAMPLES
        • JAVA EXAMPLES
        • C++ GRAPHICS PROGRAM
    • PROJECTS
      • PYTHON PROJECTS
      • SWIFT PROJECT
    • PPROGRAMMING QUIZ
    • DBMS TUTORIALS
    • COMPUTER NETWORK TUTORIALS
    • COMPUTER NETWORK SECURITY TUTORIALS
    • E COMMERCE TUTORIALS 
    • AWS TUTORIAL
    • INTERNET OF THINGS
    • CHEATSHEET
  • MORE
    • JOBS AND INTERNSHIPS
    • INTERVIEW PREPARATION
    • TECH BOOK
    • TECH NEWS
    • INSTAGRAM GALLERY
    • UNIVERSITY PAPERS
    • MNC TWEETS
    • THINKECO INITIATIVES
    • WEB STORIES
    • CONTACT US
  • WRITE +
  • ABOUT US