If there had been one grammar rule I wish I would have learned earlier on in my writing career, it would have been to learn how to punctuate dialogue. Where it’s possible to pick up many grammar rules without thinking about it, dialogue punctuation tends to be one of those unnoticed things that can then become very annoying to fix after the fact (especially if you’re a naturally dialogue-heavy writer as I’ve always been).
So, when writing dialogue, keep in mind:
1. Each speaker gets a new line.
This rule is simply to make it easier for the reader to see who is saying what. Every time you change a speaker, you will want to move to a new paragraph, e.g.
Correct:
“Hi,” Sally said.
“What’s up?” Jane asked.
Sally shrugged. “Not much.”
Incorrect
“Hi,” Sally said. “What’s up?” Jane asked. Sally shrugged. “Not much.”

Source: writersdock.com
2. Both tags and beats can be used to mark who is speaking, but they are punctuated differently.
Simply put, a tag is something connected to dialogue that is specifically how the line is said (e.g. said, asked, yelled, whispered…) and a beat (sometimes called an “action tag”) is an action that is taking place while/closely to when the line is spoken).
If you are using a tag, the tag will be treated as part of the same sentence as the dialogue and thus connected with a comma and followed by a lowercase letter (if the first word of the tag does not include a proper noun such as a name). e.g.:
Correct
“Hi,” she said.
“Howdy,” he shouted.
Incorrect
“Hi.” She said.
“Howdy.” He shouted.
If you are using a beat, however, the beat will be treated as a separate sentence and thus be separated from the dialogue with a period. The first word of the beat will be capitalized, no matter what word, much like any other sentence. e.g.:
Correct
“Hi.” She waved.
“Howdy.” He walked into the entryway.
Incorrect
“Hi,” she waved.
“Howdy,” he walked into the entryway.
Note 1: Special punctuation, such as a question mark or an exclamation point, follows the same general rule with tags/beats, acting like a comma with a tag or a period with a beat when it comes to capitalization. e.g.
Correct
“Who is she?” he asked. (tag)
“She who?” She looked around. (beat)
Incorrect
“Who is she?” He asked. (tag)
“She who?” she looked around. (beat)
Note 2: The first word inside quotation marks is always capitalized, whether it is preceded by a tag or a beat. See second example in point three.
3. A tag or beat can be used at any point in a line of dialogue.
It is possible to put a tag or beat ahead of dialogue, at the end, or even in the middle. e.g.
“Hi, Jane. When did you get here?” she said.
or
She said, “Hi, Jane. When did you get here?”
or
“Hi, Jane,” she said. “When did you get here?”
As long as it’s the same speaker, the dialogue remains on the same line no matter where the tag/beat falls. This, once again, comes down to readability. If dialogue begins to be separated from tags/beats, it can become confusing. For example, if you have:
“Hi, Jane,” she said.
“When did you get here?”
It will likely look like a new speaker (perhaps Jane replying) is saying “When did you get here?” rather than “she” continuing her line.
Note: If a line of dialogue goes on for several sentences, it is generally best to move a tag to either the start of the line or after the first or second sentence so the reader doesn’t have to get to the end of the line and then go back to attribute the line to the proper speaker.
4. If you only have two speakers going back and forth, you can drop some tags altogether.
Sometimes, if you only have two speakers in a scene, it is possible to have lines of dialogue with no tags or beats at all. For example:
“Hey,” Sally said.
“Hey.” Jane waved as she walked into the room.
“Are you staying for dinner?”
“I don’t know yet.”
“Oh.” Sally frowned. “Let me know when you do?”
As there are only Sally and Jane in the scene, even though there is no tag/beat on lines three and four, the reader can assume that Sally asks another question and then Jane answers once again. Again, though, readability trumps all. You want to be careful that you don’t go for too long a stretch with no tags–as a reader might get lost as to who was speaking what line and have to backtrack to the beginning of the conversation to figure it out–and you likely don’t want to drop may tags if you have more than two people in a conversation–as you don’t want the reader to have to struggle to figure out if it is Sally, Jane, Tom, or Steve who answered the previous line.
When dropping tags, you also want to be careful that you don’t end up with talking heads–that is, so many lines with no tags (or only quick “said” tags) that the characters are no longer grounded to the scene. Dialogue should be able to carry a lot of emotion, but don’t forget to put in tone of voice or body language when needed–or show how a character is moving around in general–or it can quickly become like reading a script without actors to deliver the lines.
All in all, when writing dialogue, making it simple for the reader to keep track of who is saying what is the most important thing. There is nothing quite like having to jump away from a line of dialogue to find out who speaking (or having to go back to re-attribute a line to a different speaker) to take a reader out of a flow of conversation. After all, in writing prose, the author needs to provide both the lines and actors–if you prefer just dialogue, consider if your story might be better off as a play.