String Operations Examples


String Operations Examples

This document introduces string formatting, splitting, joining, and encoding/decoding.

Module Introduction

Supports % formatting, str.join, str.split, etc. Built-in example:

#!pika
s = "%d" % 1
s = ",".join(['a', 'b'])
a, b = "x y".split()
#!pika

Example Code

String Formatting (strformat.py)

#!pika
assert "1" == "%d" % (1)
assert "1.0" == "%0.1f" % (1.0)
assert "1, 2" == "%s, %s" % (1, 2)
print('PASS')
#!pika

String Joining (str_join.py)

#!pika
assert ''.join([]) == ''
assert ','.join(['a', 'b']) == 'a,b'
assert '-'.join(['a', 'b', 'c']) == 'a-b-c'
assert ' '.join(['hello', 'world']) == 'hello world'
print("PASS")
#!pika

String Splitting (split.py)

#!pika
a, b = 'test asd'.split()
assert a == 'test'
assert b == 'asd'
a, b = 'test asd'.split(' ')
assert a == 'test'
assert b == 'asd'
print('PASS')
#!pika

Notes

  • Formatting placeholders support follows platform standards; split() defaults to split by whitespace.