Yep this totally fixed the same problem I had! Although for me it was more annoyance when I viewed my page source code, I could see that being an issue down the line when I get better with coding and need to assign it as a variable or something. And I was worried that it wouldn’t transfer info correctly because I’m using the same code for Meta Tags, like such:
<meta name="title" content="<?php echo trim(wp_title('', false)); ?><?php if (wp_title('', false)) { echo ' - '; } ?><?php bloginfo('name'); ?>" />
By adding your snippet of code, the meta tags come out perfectly with no extra spaces.
Thank you very much for finding that solution!