Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on String
  • Practice String
  • MCQs on String
  • Tutorial on String
  • String Operations
  • Sort String
  • Substring & Subsequence
  • Iterate String
  • Reverse String
  • Rotate String
  • String Concatenation
  • Compare Strings
  • KMP Algorithm
  • Boyer-Moore Algorithm
  • Rabin-Karp Algorithm
  • Z Algorithm
  • String Guide for CP
Open In App
Next Article:
How to Compare Strings in C#?
Next article icon

How to read or input a string?

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to learn how to print or output a string using different languages. Strings are considered a data type in general and are typically represented as arrays of bytes (or words) that store a sequence of characters. Strings are defined as an array of characters. 

Topics:

How to Read or Input a String in C

Scanf()

How to Read or Input a String in C++

cin>>

How to Read or Input a String in Java

Scanner Class

How to Read or Input a String in C#

Console.ReadLine()

How to Read or Input a String in Python

input()

Language Used

Functions or Classes

How to Read or Input a String in C

We can use the scanf() function to read a string in C language. It takes the string as an argument in double quotes (" ") with the help of "%s".

Below is the code for more reference:

C
#include <stdio.h> int main() {     char str[13];      // Reading string using scanf()     printf("Input String: ");     scanf("%s", str);     printf("\nEntered String: ");      // %s is used to print a string     printf("\"%s\"", str);     return 0; } 

Output:
Screenshot-from-2023-08-09-14-19-34

How to Read or Input a String in C++

To Read the string, just place the string (the variable that stores the string's value) after cin>>, as shown here in the following program.

Below is the code for more reference:

C++
#include <iostream> using namespace std;  //Driver code int main() {     string str;gfg            cout << "Input String: ";      // Reading input string using cin     cin >> str;      // Printing string using cout     cout << "String: ";     cout << str;     return 0; } 

Output:

Screenshot-from-2023-08-09-14-18-56

How to Read or Input a String in Java

In Java, we can use Scanner Class or InputStream to Read the string.

In below code, we will try to use the Scanner class instance to read the string.

Java
/*package whatever //do not write package name here */  import java.io.*; import java.util.*;  class GFG {     public static void main(String[] args)     {         String str;          // Declare the object and initialize with         // predefined standard input object         Scanner sc = new Scanner(System.in);          // String input         str = sc.nextLine();          // Printing string         System.out.print("String: ");         System.out.println(str);     } } 

Output:
Screenshot-from-2023-08-09-14-18-15

How to Read or Input a String in C#

A string is represented by class System.String. The “string” keyword is an alias for System.String class and instead of writing System.String one can use String which is a shorthand for System.String class.

Now to Read the String in C#, we need to use the Console.ReadLine() method, as shown below:

C#
using System;  public class GFG {      static public void Main()     {          String str;          // use ReadLine() to read the entered line         str = Console.ReadLine();          Console.Write("String: {0}", str);     } } 

Output:
Screenshot-from-2023-08-09-14-17-02

How to Read or Input a String in Python

Python input() function first takes the input from the user and converts it into a string. The type of the returned object always will be <class ‘str’>.

Python3
# Reading String using input() method name = input("Input String: ")  # Printing string using print() method print("String: ", name) 


Output:

Screenshot-from-2023-08-09-14-16-14

Reading Text Input in JavaScript

JavaScript provides various methods for capturing user input, especially when dealing with text. Here, we'll explore two common approaches: using the prompt function for simplicity and creating an HTML input element for a more interactive user experience.

1. Using prompt for Basic Input:

The prompt function allows you to gather simple text input from the user in a browser environment.

JavaScript
// Using prompt to get text input let userInput = prompt("Enter Text:");  // Displaying the input console.log("User Input:", userInput); 
Output:
GeeksforGeeks



Next Article
How to Compare Strings in C#?

S

srinam
Improve
Article Tags :
  • Strings
  • DSA
Practice Tags :
  • Strings

Similar Reads

  • How to print or output a String?
    In this article, we are going to learn how to print or output a string using different languages. Strings are considered a data type in general and are typically represented as arrays of bytes (or words) that store a sequence of characters. Strings are defined as an array of characters. Topics: Lang
    3 min read
  • Read File As String in Python
    Python provides several ways to read the contents of a file as a string, allowing developers to handle text data with ease. In this article, we will explore four different approaches to achieve this task. Each approach has its advantages and uses cases, so let's delve into them one by one. Read File
    3 min read
  • How to read each character of a string in PHP ?
    A string is a sequence of characters. It may contain integers or even special symbols. Every character in a string is stored at a unique position represented by a unique index value. Here are some approaches to read each character of a string in PHP Table of Content Using str_split() method - The st
    4 min read
  • Bash Script - Read User Input
    Interacting with users is a crucial aspect of many Bash scripts. Whether prompting for information, selecting options, or confirming actions, reading user input is essential for building powerful and flexible scripts. This guide provides a comprehensive overview of reading user input in Bash, coveri
    8 min read
  • How to Compare Strings in C#?
    A string is a collection of characters and is used to store plain text. Unlike C or C++, a string in C# is not terminated with a null character. The maximum size of a string object depends on the internal architecture of the system. A variable declared followed by "string" is actually an object of s
    13 min read
  • Convert std::string to LPCWSTR in C++
    C++ provides us a facility using which one can represent a sequence of characters as an object of a class. This class is std::string. Internally, std::string class represents a string as a sequence of characters (bytes) and each character can be accessed using the [] operator. The difference between
    2 min read
  • Program to check if input is an integer or a string
    Write a function to check whether a given input is an integer or a string. Definition of an integer : Every element should be a valid digit, i.e '0-9'. Definition of a string : Any one element should be an invalid digit, i.e any symbol other than '0-9'. Examples: Input : 127Output : IntegerExplanati
    15+ min read
  • How to Take Input in Node.js ?
    Taking input in a Node.js application is essential for building interactive command-line interfaces, processing user input, and creating dynamic applications. Node.js provides several methods for receiving input from users, including reading from standard input (stdin), command-line arguments, and u
    2 min read
  • How to split a string in C/C++, Python and Java?
    Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter. In C: // Splits str[] according to given delim
    7 min read
  • Shell Script to Split a String
    Shell Scripting or Shell Programming is just like any other programming language. A shell is a special program that provides an interface between the user and the operating system. In Linux/Unix the default shell used is bash and in windows, it is cmd(command prompt). We use the terminal to run a sh
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences