How to write “alt” tag in image for wordpress code?

Why would you want to avoid the_postthumbnail? It can give you the exact html you want if you use the right attributes. Anyway, you can access the title and alt tags of an image like this:

// get the ID of the featured image
$thumbnail_id = get_post_thumbnail_id( $post_id );
// retrieve title (an image is a post, so you can access the title as if it is a post title)
$title = get_post($thumbnail_id())->post_title;
// retrieve alt (which is stored as metadata to a post)
$alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);