Top Posts
Virtual Function in C++
Ceil and Floor functions in C++
Python Rock Paper Scissors Game
AKTU MCA 4 SEMESTER QUESTION PAPERS
Git & Github 2021 Cheat Sheet
Introduction to C Programming Language
Git & Github 2021 Cheat Sheet
10+ C Program to Print Patterns
LUCKNOW BCA 6 SEM QUESTION PAPERS
Currency Converter in Python
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
    • UNIVERSITY PAPERS
    • MNC TWEETS
    • THINKECO INITIATIVES
    • CONTACT US
  • WRITE +
  • ABOUT US
  • HIRE US
Python Tutorial

String in Python

by anupmaurya May 14, 2022
written by anupmaurya 0 comment 824 views

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

Table of Contents

  • How to Create a String in Python
      • Basic Python String
    • String Concatenation
    • String Replication
      • Math Operators
  • How to Store Strings in Variables

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’’’
Code language: Python (python)

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!”)
Code language: PHP (php)

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
Code language: Python (python)

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
Code language: Python (python)

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’
Code language: Python (python)

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”
Code language: Python (python)

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)
Code language: PHP (php)
= 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 works at 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

Python Data Types

Getting Started with Python

Python List Comprehension

Python Function

Python if else

Python Libraries

Python Dictionary

Python Input, Output, and Import

Python List

Python Operators

Leave a Comment Cancel Reply

Save my name, email, and website in this browser for the next time I comment.

* By using this form you agree with the storage and handling of your data by this website.

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

  • Virtual Function in C++

    May 19, 2022
  • Ceil and Floor functions in C++

    May 19, 2022
  • Python Rock Paper Scissors Game

    May 18, 2022
  • AKTU MCA 4 SEMESTER QUESTION PAPERS

    May 18, 2022

EDUCATIONAL

  • 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

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

  • 10 mostly asked questions related to WhatsApp

  • Top 8 Programming Languages That Will Rule in 2022

  • Google Cloud Platform

  • Best Online Code Editors For Web Developers

  • How to Write a Synopsis for Project Work

CHEATSHEET

  • Git and Github 2022 Cheat Sheet

  • ReactJs Cheatsheet

  • Linux Commands Cheat Sheet

  • C Programming language Cheatsheet

  • Scala Cheatsheet

  • MySQL Cheatsheet

  • Javascript Cheatsheet

PROJECTS

  • Python Rock Paper Scissors Game

  • Currency Converter in Python

  • Alarm clock GUI application with tkinter

  • Print emojis using python without any module

  • Country Date and Time using Python

  • Covid-19 Tracker Application Using Python

  • Python | GUI Calendar using Tkinter

  • Python: Shutdown Computer with Voice

  • Python GUI Calculator using Tkinter

  • Convert an Image to ASCII art using Python

  • Python YouTube Downloader with Pytube

  • Tic-Tac-Toe using Python

TUTORIALS

  • JAVA TUTORIAL
  • COMPUTER NETWORK
  • DBMS TUTORIAL
  • E-COMMERCE TUTORIAL
  • KNOWLEDGE MANAGEMENT
  • C++ PROGRAMMING
  • COMPUTER NETWORK SECURITY
  • AMAZON WEB SERVICES

TECH NEWS

  • PyScript: Python in the Browser

  • HalloApp is a secure alternative to WhatsApp, made by two early WhatsApp employees

  • 5+ Best Humanoid Robots In The World

  • Windows 11 Now Official, Brings Fresh UI, Centrally-Placed Start Menu

TERMS & POLICY

  • PRIVACY POLICY
  • TERMS AND CONDITIONS

COMPILERS

  • JAVA COMPILER
  • PYTHON COMPILER
  • JS COMPILER
  • C++ COMPILER
  • C COMPILER

JOBS AND INTERNSHIPS

  • TCS off-campus hiring 2022 for software engineers- 2019, 2020, & 2021 Batches

    February 27, 2022
  • Deloitte Recruitment For Any Graduates as Learning Operations Associate Analyst

    February 18, 2022
  • HP Recruitment For Tech Support Intern Position

    February 16, 2022
  • EY Hiring- PAS Global Immigration Advanced Analyst

    February 14, 2022
  • Amazon Recruitment Drive for Virtual Customer Support Associate Position

    February 12, 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
    • UNIVERSITY PAPERS
    • MNC TWEETS
    • THINKECO INITIATIVES
    • CONTACT US
  • WRITE +
  • ABOUT US
  • HIRE US