> For the complete documentation index, see [llms.txt](https://docs.bcbi.brown.edu/codiac-for-health/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.bcbi.brown.edu/codiac-for-health/computing/python/strings-and-characters.md).

# Strings and Characters

This page provides syntax for different data types in Python as well as some of their associated functions. Each section includes an example to demonstrate the described syntax or function.

## Strings <a href="#characters_and_strings" id="characters_and_strings"></a>

* A string is a sequence of one or more characters (index values start at 0)

### Some functions and index methods that can be performed on strings <a href="#some_functions_that_can_be_performed_on_strings" id="some_functions_that_can_be_performed_on_strings"></a>

| Action                                        | Function                   |
| --------------------------------------------- | -------------------------- |
| get word length                               | `len("abc")`               |
| extract nth character from word               | `"abc"[n]`                 |
| extract substring nth-mth character from word | `"abc"[n:m]`               |
| search for character in word                  | `"abc".index("character")` |
| search for subword in word                    | `"ab" in "abc"`            |
| remove white spaces from the end of a word    | `"abc   ".strip()`         |
| remove last character from word               | `"abc"[:-1]`               |
| determine data structure type                 | `type("abc")`              |

Input:

```python
# strings.py

letter = "b"
word = "good-bye"
subword = "good"

word_length = len(word)
word_first_char = word[0]
word_subword = word[5:8]

print(f"Length of word: {word_length}")
print(f"First letter: {word_first_char}")
print(f"Last three characters: {word_subword}")

print(f"{letter} is in {word}: {(word.index(letter))}")
print(f"{subword} is in {word}: {(subword in word)}")
print(f"remove the last character: {(word[:-1])}")
```

Output:

```python
Length of word: 8
First character: g
Last three characters: bye
b is in good-bye: 5
good is in good-bye: True
chop off the last character: good-by
```

## Resources <a href="#documentation" id="documentation"></a>

* W3 Schools: [Python Strings](https://www.w3schools.com/python/python_strings.asp)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.bcbi.brown.edu/codiac-for-health/computing/python/strings-and-characters.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
