# String slicing

Let's delve into the concept of string slicing and how it can be applied to extract meaningful pieces of information to be used in conversational design.

## Example use case

Consider an order ID composed of 12 digits, structured as follows:

* The first 4 digits represent the year of creation.
* The last digit denotes the type of buyer, where '4' indicates a purchase by an individual and '5' denotes a purchase by a company.
* The fifth and sixth digits represent the product category, with specific numerical codes assigned to different product types. For instance, '10' might represent a refrigerator, '25' a washing machine, '06' a computer, and so forth.

<figure><img src="/files/ZP33drA57V66P3DAuQBn" alt=""><figcaption></figcaption></figure>

Let's take the following order ID as an example. From user's utterance we have extracted an order id and **saved it as a string** to a variable named order\_id.

<figure><img src="/files/0Y662SzbJIRMVzYTPBf4" alt=""><figcaption></figcaption></figure>

Now, let's break down this order ID into its constituent parts using string slicing:

**General rules:**

1. **Slicing from the start:**
   * When slicing from the start of the string, you specify the starting index as `0`.
   * Example: `string_variable[start:]`
   * This will slice the string starting from the specified index `start` until the end of the string.
2. **Slicing from the end:**
   * When slicing from the end of the string, you can use negative indices to specify positions relative to the end of the string.
   * Example: `string_variable[-end:]`
   * This will slice the string starting from `end` positions from the end of the string until the end of the string.
3. **Slicing in an interval:**
   * You can also specify an interval (step) between characters to be included in the slice.
   * Example: `string_variable[start:end:interval]`
   * The interval specifies how many characters to skip after each character is included in the slice.
   * Omitting `start` and `end` (or both) will default to the start and end of the string, respectively.
   * Omitting `interval` will default to `1`, meaning consecutive characters will be included.
   * If `interval` is negative, the slice will be performed in reverse order.

\
Let's break it down for order\_id = "202406987614" and save each substring to a separate variable:

**Extracting the year**

* The first 4 digits in order\_id represent the year, so we have to slice the string after **4** characters from the **start** of the string.
* <mark style="color:red;">**2024**</mark>06987614
* Store value "2024" in a variable named `order_year`.

{% code title="order\_year" %}

```
order_id[:4] 
```

{% endcode %}

<figure><img src="/files/n3AgL3kJFhlRZJTlwZcb" alt=""><figcaption></figcaption></figure>

**Identifying the product category**

* The product category is coded as the **fifth and sixth** digits in the string, so we have to slice out all characters between the fifth (included) and the seventh (excluded).
* 2024<mark style="color:purple;">**06**</mark>987614
* Store value "06" in a variable `product_category`.

{% code title="product\_category" %}

```
order_id[5:7]
```

{% endcode %}

<figure><img src="/files/mjlPfiz04hRFnpDHL5Fm" alt=""><figcaption></figcaption></figure>

**Determining the buyer type**

* The last digit in a string represents buyer type, so we have to slice **1 character from the end** of a string.
* 20240698761<mark style="color:green;">**4**</mark>
* Store value "4" in a variable named `buyer_type.`

{% code title="buyer\_type" %}

```
order_id[-1]
```

{% endcode %}

<figure><img src="/files/gTAuFAndAfYe6KUXGAvY" alt=""><figcaption></figcaption></figure>

***

## Other use cases and ideas

Here are additional use cases for utilizing string slicing in chatbots and voicebots:

1. **Extracting Date and Time from ISO Format:**
   * You can use string slicing to extract different parts of the date and time from the YYYY-MM-DD-T-HH:mm.SS format.
   * For example, if you need only the date, you can use `string_variable[:10]` to slice the first 10 characters.
   * To extract the time, you can use `string_variable[11:19]` to slice the time segment.

2. **Extracting Area Code from Phone Numbers:**
   * If you have phone numbers in a format with an area code, you can use string slicing to extract this area code.
   * For instance, if the area code always consists of a certain number of digits (e.g., 3), you can use `string_variable[:3]` to slice the first three digits.

3. **Extracting Last 4 Digits from Social Security Numbers:**

   * If you need to extract a specific part from a social security number, such as the last 4 digits, you can use string slicing.
   * By using a negative index, you can easily obtain the last part of the string, for example, `string_variable[-4:]` to extract the last 4 characters.

4. **Extracting Information from Personal Identification Numbers (PINs) or Social Security Numbers (SSNs):**
   * Personal identification numbers, such as social security numbers, often encode information like date of birth and gender. String slicing can be used to extract this information.
   * For example, in some systems, the first few digits of a social security number might encode the individual's birthdate and gender.
   * By defining specific slicing patterns, you can extract the relevant portions of the SSN to obtain the date of birth and gender information.
   * For instance, you might use `string_variable[:6]` to extract the first six digits representing the birthdate, and then further processing can decode these digits to derive the actual date of birth and gender.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.borndigital.ai/for-advanced-users/conversation-design-tips/string-slicing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
