How to show a string once you submit a form in a page reloading the page?
Add the code to calculate and display the result in the form build

  public function buildForm(array $form, FormStateInterface $form_state) {
     $form['number1'] = array(
      '#type' =&gt; 'textfield',
    );
    $form['number2'] = array(
      '#type' =&gt; 'textfield',
    );

    $form['submit'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; t('Sum'),
    );

    $values = $form_state-&gt;getValues();
    if (!empty($values)) {
      $result = $values['number1'] + $values['number2'];
      $form['result'] = ['#markup' =&gt; '&lt;p&gt;' . $result . '&lt;/p&gt;'];
    }

    return $form;
  }


So that the result can be included in the form rebuild when the input values are filled in.

In submit request a rebuild:

  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    $form_state-&gt;setRebuild();
  }