Contents
String format Array.............................................................................................................................. 1
Comma Separated Array ..................................................................................................................... 2
Bracket Input ....................................................................................................................................... 3
String of space separate words ........................................................................................................... 5
2d Array Input given in String format.................................................................................................. 8
String format Array
Example Input -
Given Array without size in String Format
Ex – 1 2 3 4
Input Example
12345
1. Python
# Space-separated integers without given size
arr = list(map(int, input().split()))
print(arr)
Explanation:
input().split() splits the line by spaces → ['1','2','3','4','5']
map(int, ...) converts each to integer → [1,2,3,4,5]
2. Java
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String line = [Link](); // read entire line
String[] parts = [Link](" "); // split by space
int[] arr = new int[[Link]];
for (int i = 0; i < [Link]; i++)
arr[i] = [Link](parts[i]); // convert to int
[Link]([Link](arr));
}
}
Explanation:
We read the whole line as a string, split it by space, and parse each part to integer.
3. C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string line;
getline(cin, line); // read entire line
stringstream ss(line);
vector<int> arr;
int num;
while (ss >> num) arr.push_back(num);
for (int x : arr) cout << x << " ";
return 0;
}
Explanation:
getline reads the full line, stringstream extracts integers separated by spaces.
Comma Separated Array
1,2,3,4
and store it into an array (or list/vector) in Python, Java, and C++.
1. Python
arr = list(map(int, input().split(',')))
print(arr)
Explanation:
split(',') separates by comma → ['1','2','3','4']
map(int, ...) converts them to integers → [1, 2, 3, 4]
2. Java
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String line = [Link](); // e.g. "1,2,3,4"
String[] parts = [Link](","); // split by comma
int[] arr = new int[[Link]];
for (int i = 0; i < [Link]; i++)
arr[i] = [Link](parts[i]); // convert each to int
[Link]([Link](arr));
}
}
Explanation:
Splits the string by commas, then parses each token to integer.
3. C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string line;
getline(cin, line); // read line like "1,2,3,4"
stringstream ss(line);
vector<int> arr;
string token;
while (getline(ss, token, ',')) {
arr.push_back(stoi(token)); // convert each to integer
}
for (int x : arr) cout << x << " ";
return 0;
}
Explanation:
getline(ss, token, ',') reads tokens separated by commas; stoi converts to integer.
Bracket Input
[1, 2, 3, 4]
You have to remove the brackets and commas before converting to integers.
Here’s how to handle it cleanly in all three languages.
1. Python
s = input().strip() # "[1, 2, 3, 4]"
s = [Link]('[]') # remove square brackets
arr = list(map(int, [Link](','))) # split by comma and convert
print(arr)
Output:
[1, 2, 3, 4]
Explanation:
strip('[]') removes [ and ];
split(',') splits the numbers;
map(int, …) converts each to an integer.
2. Java
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String line = [Link]().trim(); // "[1, 2, 3, 4]"
line = [Link]("\\[|\\]", ""); // remove brackets
String[] parts = [Link](","); // split by comma
int[] arr = new int[[Link]];
for (int i = 0; i < [Link]; i++)
arr[i] = [Link](parts[i].trim());
[Link]([Link](arr));
Output:
[1, 2, 3, 4]
Explanation:
replaceAll("\\[|\\]", "") removes [ and ];
trim() clears extra spaces.
3. C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string line;
getline(cin, line); // e.g. [1, 2, 3, 4]
// remove [ and ]
[Link](remove([Link](), [Link](), '['), [Link]());
[Link](remove([Link](), [Link](), ']'), [Link]());
stringstream ss(line);
vector<int> arr;
string token;
while (getline(ss, token, ',')) {
[Link](remove([Link](), [Link](), ' '), [Link]()); // remove spaces
arr.push_back(stoi(token));
for (int x : arr) cout << x << " ";
return 0;
Output:
1234
Explanation:
Removes brackets and spaces, then splits by commas using getline().
String of space separate words
Example input:
apple banana cherry mango
You need to read it as a list/array of strings (not numbers).
1. Python
words = input().split()
print(words)
Output:
['apple', 'banana', 'cherry', 'mango']
Explanation:
split() automatically separates by spaces — no need for any conversion.
2. Java
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String line = [Link](); // e.g. "apple banana cherry mango"
String[] words = [Link](" "); // split by spaces
[Link]([Link](words));
Output:
[apple, banana, cherry, mango]
Explanation:
split(" ") splits the input string into an array of words.
3. C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string line;
getline(cin, line); // "apple banana cherry mango"
stringstream ss(line);
vector<string> words;
string word;
while (ss >> word) words.push_back(word);
for (auto &w : words) cout << w << " ";
return 0;
}
Output:
apple banana cherry mango
Explanation:
stringstream automatically separates words based on spaces.
Output with decimal points
Let’s take an example:
If you have a floating-point number (like 12.34567), you need to print it as
12.346
1. Python
num = float(input())
print(f"{num:.3f}")
Example Input: 12.34567
Output: 12.346
Explanation:
{num:.3f} rounds the number to 3 digits after the decimal.
2. Java
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
double num = [Link]();
[Link]("%.3f", num);
Example Input: 12.34567
Output: 12.346
Explanation:
[Link]("%.3f", num) prints a float rounded to 3 decimals.
3. C++
#include <bits/stdc++.h>
using namespace std;
int main() {
double num;
cin >> num;
cout << fixed << setprecision(3) << num;
return 0;
Example Input: 12.34567
Output: 12.346
Explanation:
fixed and setprecision(3) from <iomanip> ensure exactly 3 digits after the decimal point.
2d Array Input given in String format
rows = 3 , cols = 2
input = "1, 2, 3, 4, 5, 6"
You must convert the string into a 2D integer array (matrix) of size 3×2.
Here’s how to do it cleanly in Python, Java, and C++.
1. Python
rows, cols = 3, 2
data = input().strip() # e.g. "1, 2, 3, 4, 5, 6"
nums = list(map(int, [Link](',')))
matrix = []
k=0
for i in range(rows):
row = []
for j in range(cols):
[Link](nums[k])
k += 1
[Link](row)
for r in matrix:
print(r)
Example Input:
1, 2, 3, 4, 5, 6
Output:
[1, 2]
[3, 4]
[5, 6]
Explanation:
Splits by commas → converts to int → groups numbers row-wise.
2. Java
import [Link].*;
public class Main {
public static void main(String[] args) {
int rows = 3, cols = 2;
Scanner sc = new Scanner([Link]);
String line = [Link](); // "1, 2, 3, 4, 5, 6"
String[] parts = [Link](",");
int[][] matrix = new int[rows][cols];
int k = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = [Link](parts[k].trim());
k++;
// Print matrix
for (int i = 0; i < rows; i++) {
[Link]([Link](matrix[i]));
Output:
[1, 2]
[3, 4]
[5, 6]
Explanation:
Split by commas → parse integers → fill into a 2D array row by row.
3. C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int rows = 3, cols = 2;
string line;
getline(cin, line); // "1, 2, 3, 4, 5, 6"
// remove spaces
[Link](remove([Link](), [Link](), ' '), [Link]());
stringstream ss(line);
vector<int> nums;
string token;
while (getline(ss, token, ',')) {
nums.push_back(stoi(token));
int k = 0;
vector<vector<int>> matrix(rows, vector<int>(cols));
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
matrix[i][j] = nums[k++];
for (auto &row : matrix) {
for (int x : row) cout << x << " ";
cout << endl;
return 0;
Output:
12
34
56