Vyper Strings (Usage and Examples)

whiteboard crypto logo
Published by:
Whiteboard Crypto
on

A string in Vyper is a collection of characters, such as letters, numbers, and symbols, that can vary in length. Here are a few examples:

@public
username: String[50]

@external
def set_username(new_username: String[50]):

    self.username = new_username

@external
def get_username() -> String[50]:

    return self.username


@public
message: String[500]

@external
def post_message(new_message: String[500]):

    self.message = new_message

@external
def view_message() -> String[500]:

    return self.message


@public
data: String[1000]
 
@external
def store_data(_data: String[1000]):

    self.data = _data
 
@external
def get_data() -> String[1000]:

    return self.data

In this article, we’ll unravel the mysteries of strings in Vyper, from understanding what they are and how they work, to looking at a few real-life examples with contracts. We’ll also discuss some of the best practices and things to look out for when using strings in your contracts.

Overview of Strings

A string in Vyper is a collection of characters, such as letters, numbers, and symbols, that can vary in length. One of the distinguishing features of strings in Vyper is that they have a predefined size, which sets a limit to the number of characters the string can hold.

In Vyper, strings are represented by the keyword ‘String’. They are fixed-sized, meaning they can only hold strings with equal or fewer characters than the specified maximum length. This limit ensures efficient usage of memory and creates predictability in contract interactions.

In the string declaration example_str: String[100] = "Test String", ‘example_str’ is a string variable that can hold a maximum of 100 characters. It’s currently holding the string “Test String”.

Example 1: Storing and Retrieving User Names

Perhaps one of the simplest usages of strings would be to store and retrieve user names in a contract. Let’s look at the following code.

@public
username: String[50]

@external
def set_username(new_username: String[50]):
    """
    Set the username.
    """
    self.username = new_username

@external
def get_username() -> String[50]:
    """
    Get the username.
    """
    return self.username

In this contract, we declare a string variable username with a maximum length of 50 characters. We then create two functions, set_username to store a new username and get_username to retrieve the stored username.

Example 2: Creating a Message Board

Let’s look at a more complex example – a decentralized message board where users can post messages.

@public
message: String[500]

@external
def post_message(new_message: String[500]):
    """
    Post a new message to the message board.
    """
    self.message = new_message

@external
def view_message() -> String[500]:
    """
    View the latest message on the message board.
    """
    return self.message

This contract allows a user to post a message with a maximum of 500 characters to the message board, using the post_message method. The latest message can be viewed using the view_message method.

Example 3: Storing and Retrieving Data

This example shows a simple way to store and retrieve any string data.

@public
data: String[1000]
 
@external
def store_data(_data: String[1000]):
    """
    Store the data in the contract.
    """
    self.data = _data
 
@external
def get_data() -> String[1000]:
    """
    Retrieve the stored data.
    """
    return self.data

In this contract, we start by defining a string variable “data” with a maximum size of 1000 characters. We then have two functions, store_data and get_data.

The store_data function takes a String input of up to 1000 characters. This input gets stored in our data variable declared in the contract’s state. This allows us to capture and store a substantial piece of textual data in our contract.

The get_data function is used to retrieve the data we stored earlier. When called, it returns the stored string. This way, you not only store but also can retrieve your string data for additional operations or checks.

Best Practices and Potential Issues

When using strings in Vyper, here are a few things to keep in mind:

  • Be mindful of the maximum length: When declaring your string variable, be aware of the maximum length you specified. Trying to store a string larger than this will cause an error.
  • Avoid storing large strings: It can be expensive (in terms of gas cost) to store large strings in a contract. Try to keep your strings as small and concise as possible.
  • Remember strings are immutable: Strings in Vyper can’t be modified once they’re created. If you need to change a string, you’ll have to create a new one.
  • Avoid storing sensitive or valuable information in plain text that could be a potential security risk.

To sum up, strings are an invaluable part of building smart contracts in Vyper. They provide a way to store and manipulate text data efficiently, making them a must-know for any aspiring Vyper developer. Happy coding!

whiteboard crypto logo

WhiteboardCrypto is the #1 online resource for crypto education that explains topics of the cryptocurrency world using analogies, stories, and examples so that anyone can easily understand them. Growing to over 870,000 Youtube subscribers, the content has been shared around the world, played in public conferences and universities, and even in Congress.