Thursday, October 16, 2025
HomeLanguagesJavascriptp5.js str() function

p5.js str() function

The str() function in p5.js is used to convert the given boolean, string and number value into its string representation.

Syntax:

str(value)

Parameters: This function accepts single parameter value which is to be converted into its string representation. This value might be integer, float, string, boolean, negative or positive value and array of values.

Return Value: It returns the converted string representation.

Below programs illustrate the str() function in p5.js:

Example 1: This example uses str() function to convert the given input value into its corresponding string representation.




function setup() { 
   
    // Creating Canvas size
    createCanvas(600, 160); 
} 
   
function draw() { 
       
    // Set the background color 
    background(220); 
     
    // Initializing some values
    let Value1 = 12;
    let Value2 = 12.5;
    let Value3 = -7.9;
    let Value4 = -6;
    let Value5 = "6";
     
    // Calling to str() function.
    let A = str(Value1);
    let B = str(Value2);
    let C = str(Value3);
    let D = str(Value4);
    let E = str(Value5);
       
    // Set the size of text 
    textSize(16); 
       
    // Set the text color 
    fill(color('red')); 
     
    // Getting string representation
    text("String representation of value 12 is: " + A, 50, 30);
    text("String representation of value 12.5 is: " + B, 50, 60);
    text("String representation of value -7.9 is: " + C, 50, 90);
    text("String representation of value -6 is: " + D, 50, 110);
    text("String representation of string '6' is: " + E, 50, 140);
} 


Output:

Example 2: This example uses str() function to convert the given input value into its corresponding string representation.




function setup() { 
   
    // Creating Canvas size
    createCanvas(600, 140); 
} 
   
function draw() { 
       
    // Set the background color 
    background(220); 
     
    // Initializing some values
    let Value1 = true;
    let Value2 = false;
    let Value3 = "Geeks";
    let Value4 = [12, 3.6, -9.8, true, false, "Geeks"];
     
    // Calling to str() function.
    let A = str(Value1);
    let B = str(Value2);
    let C = str(Value3);
    let D = str(Value4);
       
    // Set the size of text 
    textSize(16); 
       
    // Set the text color 
    fill(color('red')); 
     
    // Getting string representation
    text("String representation of value 'true' is: " + A, 50, 30);
    text("String representation of value 'false' is: " + B, 50, 60);
    text("String representation of value 'Geeks' is: " + C, 50, 90);
    text("String representation of array of values are: " + D, 50, 110);
}  


Output:

Reference: https://p5js.org/reference/#/p5/str

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS