str

The str namespace provides string manipulation functions for working with text values in PyneCore scripts. These functions allow you to search, format, transform, and analyze strings in trading indicators and strategies.

Quick Example

  from pynecore.lib import str as pstr, label, bar_index, high, close, script

@script.indicator(title="String Operations Demo", overlay=True)
def main():
    # Get price as formatted string
    price_text: str = pstr.tostring(close)
    
    # Format with custom precision
    formatted_price: str = pstr.format("{0}", close)
    
    # String transformations
    upper_text: str = pstr.upper(price_text)
    lower_text: str = pstr.lower(upper_text)
    
    # String analysis
    length_val: int = pstr.length(price_text)
    contains_val: bool = pstr.contains(price_text, ".")
    
    # Display result
    if bar_index % 10 == 0:
        label.new(bar_index, high, upper_text)
  

Functions

contains()

Returns true if the source string contains the substring, false otherwise.

ParameterTypeDescription
sourcestrThe string to search in
str_strThe substring to search for

Returns: bool

Example:

  found: bool = str.contains("hello world", "world")  # True
  

endswith()

Returns true if the source string ends with the specified substring, false otherwise.

ParameterTypeDescription
sourcestrThe string to check
str_strThe suffix to search for

Returns: bool

Example:

  is_suffix: bool = str.endswith("example.txt", ".txt")  # True
  

format()

Creates a formatted string using a format string and arguments, supporting positional placeholders.

ParameterTypeDescription
formatStringstrFormat string with placeholders like {0}, {1}
*argsanyPositional arguments to insert into placeholders

Returns: str

Example:

  result: str = str.format("Price is {0}, Change is {1}%", 123.45, 2.5)  # "Price is 123.45, Change is 2.5%"
  

format_time()

Converts a timestamp into a string formatted according to the specified format string and timezone.

ParameterTypeDescription
timeintUnix timestamp in milliseconds
fmtstr | NoneFormat string (e.g., “yyyy-MM-dd HH:mm:ss”), default None
tzstr | NoneTimezone identifier (e.g., “America/New_York”), default None

Returns: str

Example:

  formatted: str = str.format_time(1640995200000, "yyyy-MM-dd", "UTC")  # "2022-01-01"
  

length()

Returns the number of characters in the string.

ParameterTypeDescription
sourcestrThe string to measure

Returns: int

Example:

  len_val: int = str.length("hello")  # 5
  

lower()

Returns a new string with all letters converted to lowercase.

ParameterTypeDescription
sourcestrThe string to convert

Returns: str

Example:

  lower_str: str = str.lower("HELLO World")  # "hello world"
  

match()

Returns the substring that matches the specified regex pattern, or an empty string if no match is found.

ParameterTypeDescription
sourcestrThe string to search in
regexstrRegular expression pattern to match

Returns: str

Example:

  matched: str = str.match("Price: 123.45", "[0-9]+")  # "123"
  

pos()

Returns the position of the first occurrence of the substring, or na if not found.

ParameterTypeDescription
sourcestrThe string to search in
str_strThe substring to find

Returns: int (or na if not found)

Example:

  position: int = str.pos("hello world", "world")  # 6
  

repeat()

Constructs a new string by repeating the source string multiple times with a separator between each repetition.

ParameterTypeDescription
sourcestrThe string to repeat
repeatintNumber of times to repeat
separatorstrString to insert between repetitions, default ""

Returns: str

Example:

  repeated: str = str.repeat("ab", 3, "-")  # "ab-ab-ab"
  

replace()

Returns a new string with the Nth occurrence of the target substring replaced by the replacement string.

ParameterTypeDescription
sourcestrThe string to modify
targetstrThe substring to replace
replacementstrThe replacement string
occurenceintWhich occurrence to replace (0-based), default 0

Returns: str

Example:

  replaced: str = str.replace("hello hello", "hello", "hi", 1)  # "hi hello"
  

replace_all()

Replaces every occurrence of the target substring with the replacement string.

ParameterTypeDescription
sourcestrThe string to modify
targetstrThe substring to replace
replacementstrThe replacement string

Returns: str

Example:

  replaced: str = str.replace_all("hello hello", "hello", "hi")  # "hi hi"
  

split()

Divides a string into an array of substrings using the specified separator.

ParameterTypeDescription
stringstrThe string to split
separatorstrThe delimiter to split on

Returns: array<string> (array ID)

Example:

  parts: list[str] = str.split("a,b,c", ",")  # ["a", "b", "c"]
  

startswith()

Returns true if the source string starts with the specified substring, false otherwise.

ParameterTypeDescription
sourcestrThe string to check
str_strThe prefix to search for

Returns: bool

Example:

  is_prefix: bool = str.startswith("example.txt", "example")  # True
  

substring()

Returns a new string extracted from the source, starting at the specified position and extending for the specified length.

ParameterTypeDescription
sourcestrThe string to extract from
begin_posintStarting position (0-based)
end_posint | NoneEnding position (0-based, exclusive), default None (end of string)

Returns: str

Example:

  sub: str = str.substring("hello", 1, 4)  # "ell"
  

tonumber()

Converts a string representation of a number to its float equivalent.

ParameterTypeDescription
stringstrThe string to convert

Returns: float

Example:

  num: float = str.tonumber("123.45")  # 123.45
  

tostring()

Converts a value to its string representation.

ParameterTypeDescription
valueanyThe value to convert (float, int, bool, or string)
fmtstr | FormatNumber format string, default "#.##########"

Returns: str

Example:

  str_val: str = str.tostring(123.45)              # "123.45"
str_fmt: str = str.tostring(123.45, "#.##")      # "123.45"
bool_str: str = str.tostring(True)               # "true"
  

trim()

Constructs a new string with all consecutive whitespace and control characters (such as \n, \t) removed from both ends.

ParameterTypeDescription
sourcestrThe string to trim

Returns: str

Example:

  trimmed: str = str.trim("  hello world  ")  # "hello world"
  

upper()

Returns a new string with all letters converted to uppercase.

ParameterTypeDescription
sourcestrThe string to convert

Returns: str

Example:

  upper_str: str = str.upper("hello World")  # "HELLO WORLD"
  

Compatibility

All 18 functions in the str namespace are fully implemented and compatible with TradingView’s Pine Script v6 string functions. PyneCore handles string formatting with Pine-compatible rules, including support for timezone-aware timestamp formatting via format_time().