# tf.compat.v1.strings.substr

Return substrings from `Tensor` of strings.

``````tf.compat.v1.strings.substr(
input, pos, len, name=None, unit='BYTE'
)
``````

For each string in the input `Tensor`, creates a substring starting at index `pos` with a total length of `len`.

If `len` defines a substring that would extend beyond the length of the input string, then as many characters as possible are used.

A negative `pos` indicates distance within the string backwards from the end.

If `pos` specifies an index which is out of range for any of the input strings, then an `InvalidArgumentError` is thrown.

`pos` and `len` must have the same shape, otherwise a `ValueError` is thrown on Op creation.

NOTE: `Substr` supports broadcasting up to two dimensions. More about broadcasting here

Examples

Using scalar `pos` and `len`:

``````input = [b'Hello', b'World']
position = 1
length = 3

output = [b'ell', b'orl']
``````

Using `pos` and `len` with same shape as `input`:

``````input = [[b'ten', b'eleven', b'twelve'],
[b'thirteen', b'fourteen', b'fifteen'],
[b'sixteen', b'seventeen', b'eighteen']]
position = [[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]
length =   [[2, 3, 4],
[4, 3, 2],
[5, 5, 5]]

output = [[b'en', b'eve', b'lve'],
[b'hirt', b'urt', b'te'],
[b'ixtee', b'vente', b'hteen']]
``````

Broadcasting `pos` and `len` onto `input`:

``````input = [[b'ten', b'eleven', b'twelve'],
[b'thirteen', b'fourteen', b'fifteen'],
[b'sixteen', b'seventeen', b'eighteen'],
[b'nineteen', b'twenty', b'twentyone']]
position = [1, 2, 3]
length =   [1, 2, 3]

output = [[b'e', b'ev', b'lve'],
[b'h', b'ur', b'tee'],
[b'i', b've', b'hte'],
[b'i', b'en', b'nty']]
``````

Broadcasting `input` onto `pos` and `len`:

``````input = b'thirteen'
position = [1, 5, 7]
length =   [3, 2, 1]

output = [b'hir', b'ee', b'n']
``````

#### Args:

• `input`: A `Tensor` of type `string`. Tensor of strings
• `pos`: A `Tensor`. Must be one of the following types: `int32`, `int64`. Scalar defining the position of first character in each substring
• `len`: A `Tensor`. Must have the same type as `pos`. Scalar defining the number of characters to include in each substring
• `unit`: An optional `string` from: `"BYTE", "UTF8_CHAR"`. Defaults to `"BYTE"`. The unit that is used to create the substring. One of: `"BYTE"` (for defining position and length by bytes) or `"UTF8_CHAR"` (for the UTF-8 encoded Unicode code points). The default is `"BYTE"`. Results are undefined if `unit=UTF8_CHAR` and the `input` strings do not contain structurally valid UTF-8.
• `name`: A name for the operation (optional).

#### Returns:

A `Tensor` of type `string`.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5016797,"math_prob":0.83986795,"size":2414,"snap":"2020-34-2020-40","text_gpt3_token_len":759,"char_repetition_ratio":0.14564316,"word_repetition_ratio":0.052493438,"special_character_ratio":0.31234467,"punctuation_ratio":0.23908524,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9672581,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-13T20:41:19Z\",\"WARC-Record-ID\":\"<urn:uuid:082da46a-705a-4581-afb1-ad65914ddfe8>\",\"Content-Length\":\"707358\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9078a46d-737b-4cee-add2-c0e462dd1d2a>\",\"WARC-Concurrent-To\":\"<urn:uuid:107c8eb9-0832-4e2c-a60f-3d4f9c230d2b>\",\"WARC-IP-Address\":\"172.217.15.99\",\"WARC-Target-URI\":\"https://tensorflow.google.cn/versions/r2.0/api_docs/python/tf/compat/v1/strings/substr\",\"WARC-Payload-Digest\":\"sha1:LXKVLKTKEQSGVDQRIDS2BJDN6UXUS5LV\",\"WARC-Block-Digest\":\"sha1:RMLP3SF7LNBGMFFFVGM52FFUPCM7RJAJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439739073.12_warc_CC-MAIN-20200813191256-20200813221256-00263.warc.gz\"}"}