StringPart["string",n]
gives the n
character in "string".
StringPart["string",{n1,n2,…}]
gives a list of the ni
characters in "string".
StringPart["string",m;;n;;s]
gives a list of the characters in "string" from the m
through the n
in steps of s.
StringPart[{s1,s2,…},spec]
gives the list of results for each of the si.
StringPart
StringPart["string",n]
gives the n
character in "string".
StringPart["string",{n1,n2,…}]
gives a list of the ni
characters in "string".
StringPart["string",m;;n;;s]
gives a list of the characters in "string" from the m
through the n
in steps of s.
StringPart[{s1,s2,…},spec]
gives the list of results for each of the si.
Details
- StringPart["string",-n] counts from the end to give the n
-to-last character in "string". - StringPart gives each character as a string.
- In StringPart["string",m;;n;;s], m, n, and/or s can be negative.
- StringPart[BioSequence["type","seq"],spec] will apply the operation to the string "seq" yielding BioSequence results.
Examples
open all close allBasic Examples (3)
Extract the sixth character in a string:
StringPart["abcdefghijklm", 6]Use InputForm to show quotes:
InputForm[%]Extract several characters from a string:
StringPart["abcdefghijklm", {1, 3, 5}]Extract a character from the end of a string:
StringPart["abcdefghijklm", -4]Scope (6)
Obtain the first six characters of a string:
StringPart["abcdefghijklm", 1 ;; 6]Obtain a list of every other character:
StringPart["abcdefghijklm", 1 ;; -1 ;; 2]Obtain a list of every other character, starting from the end of the string:
StringPart["abcdefghijklm", -1 ;; 1 ;; -2]Get the first character of each of several strings:
StringPart[{"abcd", "efgh", "ijklm"}, 1]Find the first and last characters:
StringPart[{"abcd", "efgh", "ijklm"}, {1, -1}]StringPart works with special characters:
StringPart["◆αβγ⟷𝒜ℬ𝒞↵", -4]Extract single-letter sequences from a DNA sequence:
StringPart[BioSequence["DNA", "CGGTGC"], 4 ;; 5]Properties & Relations (7)
StringPart[str,{}] returns the empty list:
StringPart["abcde", {}]StringPart works like applying Part to the list of characters:
StringPart["abcdefghijklm", 6]Characters["abcdefghijklm"][[6]]StringPart["abcdefghijklm", {1, 3, 5}]Characters["abcdefghijklm"][[{1, 3, 5}]]StringPart["abcdefghijklm", 1 ;; -1 ;; 2]Characters["abcdefghijklm"][[1 ;; -1 ;; 2]]The characters in the list returned by StringPart can be turned into a string using StringJoin:
StringPart["abcdefghijklm", {1, 3, 7}]StringJoin[%]StringPart[str,n] is effectively equivalent to StringTake[str,{n}]:
StringPart["abcdefghijklm", 6]StringTake["abcdefghijklm", {6}]StringPart[str,n;;m;;s] returns a list of characters:
StringPart["abcdefghijklm", 1 ;; 5 ;; 3]StringTake[str,{n,m,s}] returns the same characters as a single string:
StringTake["abcdefghijklm", {1, 5, 3}]StringPart[str,All] is effectively equivalent to Characters[str]:
StringPart["abcdef", All] === Characters["abcdef"]StringPart operates on the raw characters in a string:
StringPart["red", -20 ;; -14]FullForm["red"]Possible Issues (1)
StringPosition gives results in a form suitable for StringTake, not StringPart:
StringPosition["abcdefghijklm", "fgh"]StringTake["abcdefghijklm", {{6, 8}}]StringPart["abcdefghijklm", {{6, 8}}]See Also
Related Guides
Text
Wolfram Research (2016), StringPart, Wolfram Language function, https://reference.wolfram.com/language/ref/StringPart.html (updated 2020).
CMS
Wolfram Language. 2016. "StringPart." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/StringPart.html.
APA
Wolfram Language. (2016). StringPart. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/StringPart.html
BibTeX
@misc{reference.wolfram_2026_stringpart, author="Wolfram Research", title="{StringPart}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/StringPart.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_stringpart, organization={Wolfram Research}, title={StringPart}, year={2020}, url={https://reference.wolfram.com/language/ref/StringPart.html}, note=[Accessed: 13-June-2026]}