Showing posts with label concatenate. Show all posts
Showing posts with label concatenate. Show all posts

Monday, 22 January 2024

TypeError: can only concatenate str (not "int") to str

You will get the error 'TypeError: can only concatenate str (not "int") to str', when you use the + operator to add an integer to a string because this is ungrammatical in Python. You can use + operator to add two numbers together or concatenate two strings, but not the combination.

>>> name = 'Krishna'
>>> age = 35
>>> msg = 'Hi, my name is ' + name + ' and I am ' + age + ' years old.'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

How to solve this problem?

To concatenate a string with an integer like 35, you must convert 35 into its string representation '35'. This can be achieved by using the str() function, which takes an integer as input and returns its string equivalent, as demonstrated below:

>>> msg = 'Hi, my name is ' + name + ' and I am ' + str(age) + ' years old.'
>>> msg
'Hi, my name is Krishna and I am 35 years old.'



Previous                                                 Next                                                 Home

Thursday, 11 March 2021

Php: . : Concatenate strings

‘.’ operator is used to concatenate two strings.

 

Example

$msg = "Hello " . $name . ", you are from " . $country . " and you are " . $age . "years old";

 

Find the below working application.

 

string_concatenation_demo.php

#!/usr/bin/php

<?php
$age = 23;
$name = "Krishna";
$country = "India";

$msg = "Hello " . $name . ", you are from " . $country . " and you are " . $age . "years old";

echo $msg?>

Output

$./string_concatenation_demo.php 

Hello Krishna, you are from India and you are 23years old




  

Previous                                                    Next                                                    Home

Thursday, 13 February 2020

Java: Concatenate two byte arrays

Approach 1: Traversing to each element of array and copy.
public static byte[] concat_approach1(byte[] arr1, byte[] arr2) {
 if (arr1 == null || arr1.length == 0) {
  return arr2;
 }

 if (arr2 == null || arr2.length == 0) {
  return arr1;
 }

 int arr1Length = arr1.length;
 int arr2Length = arr2.length;
 int totalElements = arr1Length + arr2Length;

 byte[] arr3 = new byte[totalElements];

 int counter = 0;

 for (int i = 0; i < arr1Length; i++) {
  arr3[counter++] = arr1[i];
 }

 for (int i = 0; i < arr2Length; i++) {
  arr3[counter++] = arr2[i];
 }

 return arr3;
}

Approach 2: Using System.arraycopy
public static byte[] concat_approach2(byte[] arr1, byte[] arr2) {
 if (arr1 == null || arr1.length == 0) {
  return arr2;
 }

 if (arr2 == null || arr2.length == 0) {
  return arr1;
 }

 int arr1Length = arr1.length;
 int arr2Length = arr2.length;
 int totalElements = arr1Length + arr2Length;

 byte[] arr3 = new byte[totalElements];
 System.arraycopy(arr1, 0, arr3, 0, arr1Length);
 System.arraycopy(arr2, 0, arr3, arr1Length, arr2Length);

 return arr3;
}

Approach 3: Using ByteArrayOutputStream
public static byte[] concat_approach3(byte[] arr1, byte[] arr2) throws IOException {
 if (arr1 == null || arr1.length == 0) {
  return arr2;
 }

 if (arr2 == null || arr2.length == 0) {
  return arr1;
 }

 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 outputStream.write(arr1);
 outputStream.write(arr2);

 return outputStream.toByteArray();
}

Approach 4: Using ByteBuffer.
public static byte[] concat_approach4(byte[] arr1, byte[] arr2) throws IOException {
 if (arr1 == null || arr1.length == 0) {
  return arr2;
 }

 if (arr2 == null || arr2.length == 0) {
  return arr1;
 }
 
 int arr1Length = arr1.length;
 int arr2Length = arr2.length;
 
 ByteBuffer byteBuffer = ByteBuffer.allocate(arr1Length + arr2Length);
 byteBuffer.put(arr1);
 byteBuffer.put(arr2);
 
 return byteBuffer.array();
}

Approach 5: Using String concatenation.
public static byte[] concat_approach5(byte[] arr1, byte[] arr2) {
 if (arr1 == null || arr1.length == 0) {
  return arr2;
 }

 if (arr2 == null || arr2.length == 0) {
  return arr1;
 }

 String s1 = new String(arr1);
 String s2 = new String(arr2);

 String result = s1 + s2;
 return result.getBytes();
}

Find the below working application.

App.java
package com.sample.app;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class App {

 public static void printArray(byte[] arr) {
  if (arr == null || arr.length == 0) {
   return;
  }

  for (byte ele : arr) {
   System.out.print(ele + " ");
  }
  System.out.println();
 }

 public static byte[] concat_approach1(byte[] arr1, byte[] arr2) {
  if (arr1 == null || arr1.length == 0) {
   return arr2;
  }

  if (arr2 == null || arr2.length == 0) {
   return arr1;
  }

  int arr1Length = arr1.length;
  int arr2Length = arr2.length;
  int totalElements = arr1Length + arr2Length;

  byte[] arr3 = new byte[totalElements];

  int counter = 0;

  for (int i = 0; i < arr1Length; i++) {
   arr3[counter++] = arr1[i];
  }

  for (int i = 0; i < arr2Length; i++) {
   arr3[counter++] = arr2[i];
  }

  return arr3;
 }

 public static byte[] concat_approach2(byte[] arr1, byte[] arr2) {
  if (arr1 == null || arr1.length == 0) {
   return arr2;
  }

  if (arr2 == null || arr2.length == 0) {
   return arr1;
  }

  int arr1Length = arr1.length;
  int arr2Length = arr2.length;
  int totalElements = arr1Length + arr2Length;

  byte[] arr3 = new byte[totalElements];
  System.arraycopy(arr1, 0, arr3, 0, arr1Length);
  System.arraycopy(arr2, 0, arr3, arr1Length, arr2Length);

  return arr3;
 }

 public static byte[] concat_approach3(byte[] arr1, byte[] arr2) throws IOException {
  if (arr1 == null || arr1.length == 0) {
   return arr2;
  }

  if (arr2 == null || arr2.length == 0) {
   return arr1;
  }

  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  outputStream.write(arr1);
  outputStream.write(arr2);

  return outputStream.toByteArray();
 }

 public static byte[] concat_approach4(byte[] arr1, byte[] arr2) throws IOException {
  if (arr1 == null || arr1.length == 0) {
   return arr2;
  }

  if (arr2 == null || arr2.length == 0) {
   return arr1;
  }

  int arr1Length = arr1.length;
  int arr2Length = arr2.length;

  ByteBuffer byteBuffer = ByteBuffer.allocate(arr1Length + arr2Length);
  byteBuffer.put(arr1);
  byteBuffer.put(arr2);

  return byteBuffer.array();
 }

 public static byte[] concat_approach5(byte[] arr1, byte[] arr2){
  if (arr1 == null || arr1.length == 0) {
   return arr2;
  }

  if (arr2 == null || arr2.length == 0) {
   return arr1;
  }

  String s1 = new String(arr1);
  String s2 = new String(arr2);

  String result = s1 + s2;
  return result.getBytes();
 }

 public static void main(String[] args) throws IOException {
  byte[] arr1 = { 2, 3, 5, 7 };
  byte[] arr2 = { 2, 4, 6, 8, 10 };

  byte[] arr3 = concat_approach1(arr1, arr2);
  byte[] arr4 = concat_approach2(arr1, arr2);
  byte[] arr5 = concat_approach3(arr1, arr2);
  byte[] arr6 = concat_approach4(arr1, arr2);
  byte[] arr7 = concat_approach5(arr1, arr2);
  

  printArray(arr3);
  printArray(arr4);
  printArray(arr5);
  printArray(arr6);
  printArray(arr7);
 }

}

Output

2 3 5 7 2 4 6 8 10 
2 3 5 7 2 4 6 8 10 
2 3 5 7 2 4 6 8 10 
2 3 5 7 2 4 6 8 10 
2 3 5 7 2 4 6 8 10


You may like