Skip to main content
Engineering LibreTexts

10.7: Character Format Specifier

  • Page ID
    54291
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The character format specifier rAw is used tell the system exactly how many positions should be used to either read or write a character variable. The w is the width or how many total places are used. If the width is not specified, the existing length of the string is used. The r is the number of times the format specifier should be repeated.

    A format of '(a6)' would look like:

    c c c c c c
    \(\leftarrow \quad\) w \(\quad \rightarrow\)

    For example, given the declarations,

    character(len=11) :: msg = "Hello World"
    

    the following write statement can be used to display the string in variable msg with no leading or trailing spaces. The following write statement,

    write (*,'(a11)') msg
    

    will display “Hello World”. The count is not required when using the character format specifier. For example, the statement,

    write (*,'(a)') msg
    

    will display the same “Hello World” string. Multiple variables or strings can be displayed. Also, the count can be used to display a portion of the string. For example, to display the string in variable msg and the string “Goodbye cruel world”.

    write (*,'(a9,2x,a)') msg, "Goodbye cruel world"
    

    Which will display “Hello Wor Goodbye cruel world” to the screen. Note that for the first string variable, msg, only the first 9 characters of the 11 total characters are displayed since the A9 format was used.


    This page titled 10.7: Character Format Specifier is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?